How can I find an array in VB.NET?

I want to be able to efficiently search an array for the contents of a string.
Example:

dim arr() as string={"ravi","Kumar","Ravi","Ramesh"} 

I pass the value of "ra" and I want it to return index 2 and 3.

How to do it in VB.NET?

+6
arrays
source share
8 answers

It is not clear how you want to search for an array. Here are a few alternatives:

Find all elements containing the exact string "Ra" (returns points 2 and 3):

 Dim result As String() = Array.FindAll(arr, Function(s) s.Contains("Ra")) 

Find all elements starting with the exact string "Ra" (returns points 2 and 3):

 Dim result As String() = Array.FindAll(arr, Function(s) s.StartsWith("Ra")) 

Find all elements containing any version of "ra" (returns points 0, 2, and 3):

 Dim result As String() = Array.FindAll(arr, Function(s) s.ToLower().Contains("ra")) 

Find all items starting with any version of ra version (retuns items 0, 2, and 3):

 Dim result As String() = Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra")) 

-

If you are not using VB 9+, then you do not have anonymous functions, so you need to create a named function.

Example:

 Function ContainsRa(s As String) As Boolean Return s.Contains("Ra") End Function 

Using:

 Dim result As String() = Array.FindAll(arr, ContainsRa) 

Having a function that can compare only with a specific string is not always very useful, so to be able to specify a string to compare with you, you will have to put it in a class to store the string somewhere:

 Public Class ArrayComparer Private _compareTo As String Public Sub New(compareTo As String) _compareTo = compareTo End Sub Function Contains(s As String) As Boolean Return s.Contains(_compareTo) End Function Function StartsWith(s As String) As Boolean Return s.StartsWith(_compareTo) End Function End Class 

Using:

 Dim result As String() = Array.FindAll(arr, New ArrayComparer("Ra").Contains) 
+14
source share
 Dim inputString As String = "ra" Enumerable.Range(0, arr.Length).Where(Function(x) arr(x).ToLower().Contains(inputString.ToLower())) 
+3
source share

If you need an efficient search that often repeats, first sort the array ( Array.Sort ) and then use Array.BinarySearch .

+2
source share

If you were looking for an older version of .NET, use:

 Module Module1 Sub Main() Dim arr() As String = {"ravi", "Kumar", "Ravi", "Ramesh"} Dim result As New List(Of Integer) For i As Integer = 0 To arr.Length If arr(i).Contains("ra") Then result.Add(i) Next End Sub End Module 
+2
source share

compare the properties in the array, if it matches the input, and then set something to the value of the current position of the loops, which is also the index of the currently viewed item.

simple for example.

 dim x,y,z as integer dim aNames, aIndexes as array dim sFind as string for x = 1 to length(aNames) if aNames(x) = sFind then y = x 

y is the index of the element in the array, then the loop can be used to store them in the array, so instead of the above, you should:

 z = 1 for x = 1 to length(aNames) if aNames(x) = sFind then aIndexes(z) = xz = z + 1 endif 
+1
source share

check this.

  string[] strArray = { "ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI" }; Array.IndexOf(strArray, "C"); // not found, returns -1 Array.IndexOf(strArray, "CDE"); // found, returns index 
+1
source share

Vb

 Dim arr() As String = {"ravi", "Kumar", "Ravi", "Ramesh"} Dim result = arr.Where(Function(a) a.Contains("ra")).Select(Function(s) Array.IndexOf(arr, s)).ToArray() 

FROM#

 string[] arr = { "ravi", "Kumar", "Ravi", "Ramesh" }; var result = arr.Where(a => a.Contains("Ra")).Select(a => Array.IndexOf(arr, a)).ToArray(); 

----- Detailed ------

 Module Module1 Sub Main() Dim arr() As String = {"ravi", "Kumar", "Ravi", "Ramesh"} Dim searchStr = "ra" 'Not case sensitive - checks if item starts with searchStr Dim result1 = arr.Where(Function(a) a.ToLower.StartsWith(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray 'Case sensitive - checks if item starts with searchStr Dim result2 = arr.Where(Function(a) a.StartsWith(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray 'Not case sensitive - checks if item contains searchStr Dim result3 = arr.Where(Function(a) a.ToLower.Contains(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray Stop End Sub End Module 
0
source share

This will do the trick by returning values ​​in the values ​​0, 2, and 3.

 Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra")) 
-one
source share

All Articles