Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>IndexOf</code> searches for exact match. To customize the matching criteria, you can use the <code>FindIndex</code> method:</p> <pre><code>// Finds first element of the array that contains `inputString` Array.FindIndex(arr, Function(s) s.Contains(inputString)) // Finds first element of the array that begins with `inputString` Array.FindIndex(arr, Function(s) s.StartsWith(inputString)) </code></pre> <p><strong>Edit to clarify <code>Function(s)</code> and lambdas:</strong> </p> <p><code>Array.FindIndex</code> takes two arguments, the first of which is an array you want to work on, the latter is a <code>Delegate</code> representing the predicate to check.</p> <p><code>FindIndex</code> does not have any idea about what kind of element you want. It gives you the flexibility to specify it. You tell it by passing in a function that takes an array element and returns a boolean indicating whether you want it or not. It essentially calls that function with every element of the array and returns the index of the first element for which that function returns true.</p> <p>Instead of writing a whole function and passing it using <code>AddressOf MyPredicate</code>, we can easily use the <code>Function(s) s.Contains(inputString)</code>. It's equivalent to:</p> <pre><code>Function MyPredicate(s As String) As Boolean Return s.Contains(inputString) End Function Array.FindIndex(arr, AddressOf MyPredicate) </code></pre> <p>Of course if we did that, we had to store <code>inputString</code> somewhere accessible to that method. It's a lot of dirty code. A lambda expression reduces all these hassles.</p> <p><strong>Edit 2:</strong></p> <p>I can verify this code prints "1":</p> <pre><code>Module Module1 Sub Main() Dim arr() As String = {"ravi", "somu", "arul"} Console.WriteLine(Array.FindIndex(arr, Function(s) s.Contains("so"))) End Sub End Module </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload