C# String IndexOfAny(Char[], Int32, Int32)

Description

String IndexOfAny(Char[], Int32, Int32) reports the zero-based index of the first occurrence in this instance 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.

Syntax

String.IndexOfAny(Char[], Int32, Int32) has the following syntax.


public int IndexOfAny(
  char[] anyOf,//from w ww  .  j a v  a  2 s  . co  m
  int startIndex,
  int count
)

Parameters

String.IndexOfAny(Char[], Int32, Int32) has the following parameters.

  • anyOf - A Unicode character array containing one or more characters to seek.
  • startIndex - The search starting position.
  • count - The number of character positions to examine.

Returns

String.IndexOfAny(Char[], Int32, Int32) method returns The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

Example

Sample for String.IndexOfAny(Char[], Int32, Int32)


//  ww w.j  a va2 s  . c o  m
using System;

class Sample {
    public static void Main() 
    {
        string str = "This is a test";
        int start;
        int at;
        int count;
        string target = "a";
        char[] anyOf = target.ToCharArray();
    
        start = (str.Length-1)/3;
        count = (str.Length-1)/4;
    
        at = str.IndexOfAny(anyOf, start, count);
        if (at > -1) 
            Console.Write(at);
        else
            Console.Write("(not found)");
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version