C# String IndexOfAny(Char[])

Description

String IndexOfAny(Char[]) reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.

Syntax

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


public int IndexOfAny(
  char[] anyOf
)

Parameters

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

  • anyOf - A Unicode character array containing one or more characters to seek.

Returns

String.IndexOfAny(Char[]) 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

The following example uses the IndexOfAny method to check for invalid characters in a user entered string.


/*from   w  w  w  . java2  s.c  o  m*/

using System;
public class MainClass
{
    public static void Main(String[] argv)
    {
        System.Console.WriteLine("java2s.com".IndexOfAny(new char[] { 'a' }));
    }
}

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