Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search String for Items in Array 2

Status
Not open for further replies.

jalbao

Programmer
Nov 27, 2000
413
US
What is the most efficient way to loop thru each char of a string and check to see if the char in scope is in an array?

It would be cool if I could do something like the code below:

Code:
Dim MyString As String = "The quick brown fox"
Dim FindTheseChars() As String = {"q","x","w"}

For i as integer = 0 To MyString.Length - 1
If MyString.Char(i) = AnyItem In FindTheseChars Then
'Do something
End If
Next

Note: I'm trying to avoid using OR statements for each of the items in the array because my array has many items.
 
Try IndexOfAny method
Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters.

Overload List
Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters.

Overloads Public Function IndexOfAny(Char()) As Integer

Dim MyString As String = "The quick brown fox"
Dim FindTheseChars() As Char = {"q"c,"x"c,"w"c}

Dim i as Integer = -1
Dim cFound as Char

Do
i +=1
i = MyString.indexofAny(FindTheseChars,I)
if I < 0 then exit do
cFound = MyString.Chars(i)
' do something
Loop




Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Code:
Dim MyString As String = "The quick brown fox"
Dim FindTheseChars() As String = {"q","x","w"}

For i as integer = 0 To MyString.Length - 1
     For Each s As String In FindTheseChars
          If MyString.Char(i) = s Then
               'Do something
          End If
     Next
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top