添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
天涯  ·  Python 实现Trie - 简书·  1 年前    · 
天涯  ·  Deleting an element ...·  3 年前    · 
lichail  ·  Convert JSON string ...·  3 年前    · 
lichail  ·  Javascript ...·  3 年前    · 
淡定的小刀  ·  springboot ...·  2 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
    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)
    End Sub
End Module
                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 Weitzhandler
                Apr 4, 2009 at 22:11
        string[] strArray = { "ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI" };
        Array.IndexOf(strArray, "C"); // not found, returns -1
        Array.IndexOf(strArray, "CDE"); // found, returns index

compare properties in the array if one matches the input then set something to the value of the loops current position, which is also the index of the current looked up item.

simple eg.

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 then the index of the item in the array, then loop could be used to store these in an array also so instead of the above you would have:

z = 1
for x = 1 to length(aNames)
    if aNames(x) = sFind then 
        aIndexes(z) = x 
        z = z + 1
    endif
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()
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
    End Sub
End Module
                That scales badly, as it will loop through the array one extra time for each match found.
– Guffa
                Mar 30, 2009 at 14:35
                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 Weitzhandler
                Mar 30, 2009 at 15:08
                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
                Mar 31, 2009 at 4:31
                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 Weitzhandler
                Apr 4, 2009 at 22:09

Never use .ToLower and .ToUpper.

I just had problems in Turkey where there are 4 "i" letters. When using ToUpper I got the wrong "Ì" one and it fails.

Use invariant string comparisons: Const LNK as String = "LINK" Dim myString = "Link"

If myString.ToUpper = LNK Then...

Good and works in the entire world: If String.Equals(myString, LNK , StringComparison.InvariantCultureIgnoreCase) Then...

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 Mar 31, 2009 at 4:31 Dim result2 = arr.Where(Function(a) a.StartsWith(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray – somu Mar 31, 2009 at 4:33 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 Mar 31, 2009 at 14:03

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.