IndexOfAny Method (Char[], Int32, Int32)

String.IndexOfAny Method (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.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public int IndexOfAny(
	char[] anyOf,
	int startIndex,
	int count


Type: System.Char[]

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

Type: System.Int32

The search starting position.

Type: System.Int32

The number of character positions to examine.

Return Value

Type: System.Int32

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.

Exception Condition

anyOf is null.


count or startIndex is negative.


count + startIndex is greater than the number of characters in this instance.

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 the string instance.

The search for anyOf is case-sensitive.

This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar value are the same. To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

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

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group
   // Change the XML attribute name.
   [XmlAttribute(AttributeName = "MgrName")]
   public string Name;
   /* Use the AttributeName to collect all the XML attributes
   in the XML-document instance. */

public class Run
   public static void Main()
      Run test = new Run();
      /* To use the AttributeName to collect all the
      XML attributes. Call SerializeObject to generate 
      an XML document and alter the document by adding
      new XML attributes to it. Then comment out the SerializeObject
      method, and call DeserializeObject. */
public void SerializeObject(string filename)
   // Create an instance of the XmlSerializer class.
   XmlSerializer mySerializer =  new XmlSerializer(typeof(Group));
   // Writing the file requires a TextWriter.
   TextWriter writer = new StreamWriter(filename);
   // Create an instance of the class that will be serialized.
   Group myGroup = new Group();
   /* Set the Name property, which will be generated
   as an XML attribute. */
   myGroup.Name = "Wallace";
   // Serialize the class, and close the TextWriter.
   mySerializer.Serialize(writer, myGroup);

   public void DeserializeObject(string filename)
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group)

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft