tags:

views:

7941

answers:

6

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

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

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

How can I do this in VB.NET?

+2  A: 
Dim inputString As String = "ra"
Enumerable.Range(0, arr.Length).Where(Function(x) arr(x).ToLower().Contains(inputString.ToLower()))
Mehrdad Afshari
Added that into the post
Joel Coehoorn
Corrected. I confused the parameter `x` with `inputString`
Mehrdad Afshari
A: 

This would do the trick, returning the values at indeces 0, 2 and 3.

Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra"))
Dustin Campbell
This won't return the indices but the actual elements, which is not the OP specifies.
Mehrdad Afshari
True, true. Ah well...
Dustin Campbell
Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra"))I have used your code in VB.NET 2005 but i am getting error in Function keyword. It is not accepting, please help
somu
Dim result2 = arr.Where(Function(a) a.StartsWith(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray
somu
This code would only work in VB 9.0+ (i.e. Visual Studio 2008). You should always specify which version you are using in your question.
Dustin Campbell
+1  A: 

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

Konrad Rudolph
+1  A: 

It's not exactly clear how you want to search the array. Here are some alternatives:

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

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

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

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

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

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

Find all items starting with any case version of "ra" (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 don't have anonymous functions, so you have to create a named function.

Example:

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

Usage:

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

Having a function that only can compare to a specific string isn't always very useful, so to be able to specify a string to compare to you would have to put it in a class to have somewhere to store the string:

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

Usage:

Dim result As String() = Array.FindAll(arr, New ArrayComparer("Ra").Contains)
Guffa
I think he was looking for the index
Shimmy
Yes, perhaps... That isn't very clear either.
Guffa
A: 

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()

C#

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
Shimmy
That scales badly, as it will loop through the array one extra time for each match found.
Guffa
The problem is that he wants the index, he doesn't want the values.
Shimmy
There is no property Index in an array child.It's actually a good idea, all the items in an array should have a property Array to access the array and Index to have the index of this item in the array...
Shimmy
Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra"))I have used your code in VB.NET 2005 but i am getting error in Function keyword. It is not accepting, please help
somu
I posted another code which is applicaple in .NET 2.0, the previous code was for 3.0+ (maybe only 3.5 donno, but not for 3.0).
Shimmy
And if your concern is performance rather than code readability then use the code in the following reply, simple For loop this will iterate just ince as you wanted.
Shimmy
+2  A: 

In case you were looking for an older version of .NET then 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
Shimmy
Don't forget ToLower... the algorithm as written would not return 2 and 3, only 0.
cdmckay
That's his choice As well as Contains or ForEach;He didn't really say if he wants his check to ignore case, or if he wants his query to be "StartsWith" or "Contains" function, that's his choice.Look on previous post.
Shimmy