String.IndexOfAny Method (Char[])
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Parameters
- anyOf
- Type: System.Char[]
A Unicode character array containing one or more characters to seek.
Return Value
Type: System.Int32The 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 |
|---|---|
| ArgumentNullException | anyOf is null. |
Index numbering starts from zero.
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 uses the IndexOfAny method to check for invalid characters in a user entered string.
/* Get the tree node under the mouse pointer and save it in the mySelectedNode variable. */ private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { mySelectedNode = treeView1.GetNodeAt(e.X, e.Y); } private void menuItem1_Click(object sender, System.EventArgs e) { if (mySelectedNode != null && mySelectedNode.Parent != null) { treeView1.SelectedNode = mySelectedNode; treeView1.LabelEdit = true; if(!mySelectedNode.IsEditing) { mySelectedNode.BeginEdit(); } } else { MessageBox.Show("No tree node selected or selected node is a root node.\n" + "Editing of root nodes is not allowed.", "Invalid selection"); } } private void treeView1_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) { if (e.Label != null) { if(e.Label.Length > 0) { if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1) { // Stop editing without canceling the label change. e.Node.EndEdit(false); } else { /* Cancel the label edit action, inform the user, and place the node in edit mode again. */ e.CancelEdit = true; MessageBox.Show("Invalid tree node label.\n" + "The invalid characters are: '@','.', ',', '!'", "Node Label Edit"); e.Node.BeginEdit(); } } else { /* Cancel the label edit action, inform the user, and place the node in edit mode again. */ e.CancelEdit = true; MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", "Node Label Edit"); e.Node.BeginEdit(); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.