CorString: IndexOfAny

IndexOfAny

Reports the zero-based index of the first occurrence in s of any character in a specified array of Unicode characters. The search starts at a specified character position and examines a specified number of character positions.



 Public Function IndexOfAny(
	  ByRef s As String,
	  ByRef OfAny As Variant,
	  Optional ByRef StartIndex As Variant,
	  Optional ByRef Count As Variant ) As Long

Parameters

s
[ByRef] String. The string to be searched.
OfAny
[ByRef] Variant. A Unicode character array containing one or more characters to seek. This accepts String or Integer().
StartIndex
[ByRef] Optional. Variant. The zero-based starting index of s to begin searching.
Count
[ByRef] Optional. Variant. The number of characters in s to search.

Return Values

Long -  The zero-based index position of the first occurrence in s where any character in AnyOf was found; -1 if no character in AnyOf was found.

Remarks

The search begins at StartIndex and continues to StartIndex + Count - 1. The character at StartIndex + Count is not included in the search.

Index numbering starts from zero. The StartIndex parameter can range from 0 to one less than the length of s.

The search for AnyOf is case-sensitive.

Exceptions

ExceptionCondition
ArgumentNullException AnyOf is uninitialized.
ArgumentOutOfRangeException Count or StartIndex is negative.
-or-
Count + StartIndex is greater than the number of characters in s.

Examples

The following example finds the index of the occurrence of any character of the string "aid" within a substring of another string.

Public Sub Main()
    Const Br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
    Const Br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
    Const Str As String = "Now is the time for all good men to come to the aid of their party."
    Const Target As String = "aid"

    Dim Start   As Long
    Dim At      As Long
    Dim Count   As Long
    
    Start = (Len(Str) - 1) \ 3
    Count = (Len(Str) - 1) \ 4
    
    Debug.Print CorString.Format("The first character occurrence from position {0} for {1} characters.", Start, Count)
    Debug.Print CorString.Format("{0}{1}{0}{2}{0}{3}{0}", Environment.NewLine, Br1, Br2, Str)
    Debug.Print CorString.Format("A character in '{0}' occurs at position: ", Target);
    
    At = CorString.IndexOfAny(Str, Target, Start, Count)
    
    If At > -1 Then
        Debug.Print At
    Else
        Debug.Print "(not found)"
    End If
End Sub

' This example produces the following output.
'
'    The first character occurrence from position 22 for 16 characters.
'
'    0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'    0123456789012345678901234567890123456789012345678901234567890123456
'    Now is the time for all good men to come to the aid of their party.
'
'    A character in 'aid' occurs at position:  27

See Also

Project CorLib Overview

Class CorString Overview

LastIndexOfAny