首页 \ 问答 \ 搜索数组返回部分匹配(Search an array return partial matches)

搜索数组返回部分匹配(Search an array return partial matches)

我需要为一个字符串搜索关联数组的值,但只能搜索字符串示例的开始部分:

var stack = ['aba', 'abcd', 'ab', 'da', 'da'];

搜索堆栈中的值a会返回['abc, 'abcd', 'ab'] ,对于b只会返回b,而搜索'd'会返回[da', 'da'] ...任何方式来做到这一点?

我试图做一个自动完成选择框,但它的自定义,所以我需要修改文本事件和搜索我的项目数组以获得第一个匹配的索引,而用户正在键入。


I need to search an associative array's values for a string, but only the beginning of the string example:

var stack = ['aba', 'abcd', 'ab', 'da', 'da'];

a search on stack for the value a would return ['abc, 'abcd', 'ab'], and for b would just return b while a search for 'd' would return [da', 'da'] ...any way to do that?

Im trying to do like an autocomplete select box, but its custom so i need to moditor text events and search my array of items to get the index of the first match while the user is typing.


原文:https://stackoverflow.com/questions/5440275
更新时间:2021-09-21 09:09

最满意答案

由于这已经证明是不可能的,我最终将第二个动态生成的表单浮动到顶部,Opacity设置为0.01。 然后,我使用主窗体上的Resize和LocationChanged事件来调整大小并根据需要移动叠加窗体。 然后我只需要根据需要处理叠加表单点击事件。

public MainFrom()
    {
        InitializeComponent();

        f = new Form();
        f.Size = panel1.Size;
        f.FormBorderStyle = FormBorderStyle.None;
        f.BackColor = Color.Black;
        f.Opacity = 0.01;
        f.ShowInTaskbar = false;
        f.Show(this);

        f.Click += new System.EventHandler(this.UserClicked);
    }

然后,此代码调整叠加表单的大小以考虑主窗体边框:

private void SetLocation()
        {
            // These next 2 lines calculate the width of the border 
            // and the title bar and then use these to off set the floating
            // forms location correctly.
            int xOffset = (this.Width - this.ClientSize.Width) / 2;
            int yOffset= this.Height - this.ClientSize.Height - xOffset;
            int x = this.Location.X + panel1.Location.X + xOffset;
            int y = this.Location.Y + panel1.Location.Y + yOffset;
            f.Location = new Point(x, y);
        }

根据MoMa工具,这是100%单声道兼容,代码按照预期编译在MonoDevelop中。 但是,如果我运行这个,我看到一个空引用异常,我没有在加载的MS .NET中看到,这可能是可修复的,但我还没有调查。


As this has turned out to be impossible I ended up floating a 2nd dynamically generated form over the top with Opacity set to 0.01. I then used the Resize and LocationChanged events on the main form to resize and move the overlay form as required. I then just needed to handle the overlay forms click event to do as I needed.

public MainFrom()
    {
        InitializeComponent();

        f = new Form();
        f.Size = panel1.Size;
        f.FormBorderStyle = FormBorderStyle.None;
        f.BackColor = Color.Black;
        f.Opacity = 0.01;
        f.ShowInTaskbar = false;
        f.Show(this);

        f.Click += new System.EventHandler(this.UserClicked);
    }

This code then resizes the overlay form to account for the main forms border:

private void SetLocation()
        {
            // These next 2 lines calculate the width of the border 
            // and the title bar and then use these to off set the floating
            // forms location correctly.
            int xOffset = (this.Width - this.ClientSize.Width) / 2;
            int yOffset= this.Height - this.ClientSize.Height - xOffset;
            int x = this.Location.X + panel1.Location.X + xOffset;
            int y = this.Location.Y + panel1.Location.Y + yOffset;
            f.Location = new Point(x, y);
        }

According to the MoMa tool this is 100% mono compatible and the code compiles in MonoDevelop as expected. However if I run this I am seeing a null reference exception I do not see in MS .NET on load, this might be fixable but I am yet to look into this.

相关问答

更多
  • 对于vim: highlight Normal ctermbg=Black highlight NonText ctermbg=Black 你的colorschema很可能没有设置ctermbg。 将“黑色”替换为所需的背景颜色。 For vim: highlight Normal ctermbg=Black highlight NonText ctermbg=Black Your colorschema most likely does not set ctermbg. Replace "Black" ...
  • 由于这已经证明是不可能的,我最终将第二个动态生成的表单浮动到顶部,Opacity设置为0.01。 然后,我使用主窗体上的Resize和LocationChanged事件来调整大小并根据需要移动叠加窗体。 然后我只需要根据需要处理叠加表单点击事件。 public MainFrom() { InitializeComponent(); f = new Form(); f.Size = panel1.Size; f.FormBorderSt ...
  • 更改id =“page”的div的背景颜色 例如 #page{ background-color: rgba(185, 178, 178, 0.71); } 根据需要更改颜色。 希望这能解决你的问题 change the background color of the div with id="page" for example #page{ background-color: rgba(185, 178, 178, 0.71); } change the color as needed. Hope t ...
  • 感谢Hans Passant指出我正确的方向。 我最终创建了一个线程,检查鼠标是否每隔50ms处于边界内。 public Detached(PictureBox map) { Thread HoverCheck = new Thread(() => { while (true) { if (this.Bounds.Contains(Cursor.Position)) { Toggl ...
  • 这是一个简单的技巧。 添加同一帧的子视图并更改为alpha。 UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)]; UIView *bgView = [[UIView alloc] initWithFrame:popupView.frame]; UIColor * bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_bg. ...
  • 如果我理解正确,你想在父div打开一个窗口/洞。 据我所知,你不能这样做,因为div在文本框后面,并且你应用它们的任何透明度都会导致div显示在框中,而不是div后面的内容。 也许,而不是背景颜色,你可以使div背景成为一个PNG图像,其中两个透明孔与文本框重合,但这是一种解决方法,与你的一样。 If I understand correctly, you want to open a window/hole in the parent div. As far as I know, you can't do ...
  • 我在这里找到了解决方案 @Override protected void dispatchDraw(Canvas canvas) { // draw here if you want to draw behind children super.dispatchDraw(canvas); // draw here if you want to draw over children } I found the solution here @Override protected void ...
  • 您可以设置imageView的属性: imgView.contentMode = UIViewContentModeScaleToFill; imgView.clipsToBounds = YES; 在这种情况下,imageviews将定义它们之间没有任何空格,尽管可以更改宽高比如果UIViewContentModeScaleAspectFill不符合您的要求,请尝试UIViewContentModeScaleAspectFill You may set the imageView's propertie ...
  • 目前,CSS不允许更改背景图像的alpha值。 如果您希望解决此问题,有几种不同的选择: 只需将背景图像保存为适当不透明度的透明png即可。 使背景图像成为相对定位的父容器中自己绝对定位的元素。 。
    some text
    At the moment, CSS does not allow for a change i ...
  • 使用纯css是不可能的,因为你需要使绿色div也是透明的,这显然不是。 解决方法是让您的小方块与您想要的背景相同,然后使用background-position将图像移动到您想要的位置 .blue { width: 200px; height: 200px; } .blue, .same_as_blue { background: url(http://lorempixel.com/200/200/) left top no-repeat; } .green ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。