vb.net InStr 数组在字符串中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16213498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:23:29  来源:igfitidea点击:

InStr array is in string

vb.net

提问by XandrUu

Currently I'm using InStr to find a string in a string, I'm new to VB.NET and wondering if I can use InStr to search every element of an array in a string, or a similar function like this:

目前我正在使用 InStr 在字符串中查找字符串,我是 VB.NET 的新手,想知道是否可以使用 InStr 来搜索字符串中数组的每个元素,或者类似这样的函数:

InStr(string, array)

Thanks.

谢谢。

回答by SysDragon

You need to loop:

你需要循环:

Dim bFound As Boolean = False
For Each elem As String In array
    If myString.Contains(elem) Then
        bFound = True
        Exit For
    End If
Next

You can transform it into a function to call it more than once easily:

您可以将其转换为一个函数以轻松地多次调用它:

Public Function MyInStr(myString As String, array() As String) As Boolean
    For Each elem As String In array
        If myString.Contains(elem) Then return True
    Next

    return false
End Function

Then:

然后:

MyInStr("my string text", New String() {"my", "blah", "bleh"})

回答by Neolisk

Here goes the LINQ solution:

这是 LINQ 解决方案:

Dim a() = {"123", "321", "132"}
Dim v = a.Select(Function(x) InStr(x, "3")).ToArray
MessageBox.Show(String.Join(",", v)) '3,1,2

回答by Kangkan

If you are looking at searching for a string in any of the items in a string array, then you can use array.find(<T>)method. See more here: http://msdn.microsoft.com/en-IN/library/d9hy2xwa%28v=vs.90%29.aspx

如果您要在字符串数组的任何项目中搜索字符串,则可以使用array.find(<T>)方法。在此处查看更多信息:http: //msdn.microsoft.com/en-IN/library/d9hy2xwa%28v=vs.90%29.aspx

回答by Lingasamy Sakthivel

Instrreturns an integer specifying the start position of the first occurrence of one string within another.

Instr返回一个整数,指定一个字符串在另一个字符串中第一次出现的起始位置。

Refer this

参考这个

To find string in a string you can use someother method

要在字符串中查找字符串,您可以使用其他方法

Here is an example of highlighting all the text you search for at the same time but if that is not what you want, you have to solve it on your own.

这是同时突出显示您搜索的所有文本的示例,但如果这不是您想要的,则必须自己解决。

Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
Dim textEnd As Integer = rtb.TextLength
Dim index As Integer = 0
Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
While (index < lastIndex)
  rtb.Find(searchtext, index, textEnd, RichTextBoxFinds.WholeWord)
  rtb.SelectionFont = fnt
  rtb.SelectionLength = searchtext.Length
  rtb.SelectionColor = Color.Red
  rtb.SelectionBackColor = Color.Cyan
  index = rtb.Text.IndexOf(searchtext, index) + 1
End While
End Sub

This method with search for text "boy" in RichTextBox2, change the textcolor to red and back color to cyan

此方法在 RichTextBox2 中搜索文本“boy”,将文本颜色更改为红色,将背景颜色更改为青色

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
 findTextAndHighlight("boy", RichTextBox2)   
 End Sub

回答by gpinkas

Converting SysDragon's answer to classic asp:

将 SysDragon 的答案转换为经典的 asp:

You need to loop:

你需要循环:

Dim bFound
bFound = False

For Each elem In myArray
    If InStr(myString, elem)>=0 Then
        bFound = True
        Exit For
    End If
Next

You can transform it into a function to call it more than once easily:

您可以将其转换为一个函数以轻松地多次调用它:

Function MyInStr(myString, myArray)
    Dim bFound
    bFound = false

    For Each elem In myArray
        If InStr(myString, elem)>=0 Then
            bFound = True
            Exit For
        End If
    Next

    MyInStr = bFound
End Function

Then:

然后:

MyInStr("my string text", Array("my", "blah", "bleh"))