Export (0) Print
Expand All

String.IndexOfAny Method (Char[])

Reports the 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)

public int IndexOfAny (
	char[] anyOf
)
public int IndexOfAny (
	char[] anyOf
)
public function IndexOfAny (
	anyOf : char[]
) : int
Not applicable.

Parameters

anyOf

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

Return Value

The index position of the first occurrence in this instance where any character in anyOf was found; otherwise, -1 if no character in anyOf was found.

Exception typeCondition

ArgumentNullException

anyOf is a null reference (Nothing in Visual Basic).

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 code 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();
     }
     this.treeView1.LabelEdit = false;
   }
}

/* 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.get_X(), e.get_Y());
} //treeView1_MouseDown

private void menuItem1_Click(Object sender, System.EventArgs e)
{
    if (mySelectedNode != null && mySelectedNode.get_Parent() != null) {
        treeView1.set_SelectedNode(mySelectedNode);
        treeView1.set_LabelEdit(true);
        if (!(mySelectedNode.get_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");
    }
} //menuItem1_Click

private void treeView1_AfterLabelEdit(Object sender, 
                            System.Windows.Forms.NodeLabelEditEventArgs e)
{
    if (e.get_Label()!= null) {
        if (e.get_Label().length() > 0) {
            if (e.get_Label().IndexOfAny((new char[]{ '@', '.', ',', '!' }))
                == -1) {
                // Stop editing without canceling the label change.
                e.get_Node().EndEdit(false);
            }
            else {
                /* Cancel the label edit action, inform the user, and 
                   place the node in edit mode again. 
                 */
                e.set_CancelEdit(true);
                MessageBox.Show("Invalid tree node label.\n" 
                    + "The invalid characters are: "
                    + "'@','.', ',', '!'", "Node Label Edit");
                e.get_Node().BeginEdit();
            }
        }
        else {
            /* Cancel the label edit action, inform the user, and 
               place the node in edit mode again.
             */
            e.set_CancelEdit(true);
            MessageBox.Show("Invalid tree node label.\n"
                + "The label cannot be blank", "Node Label Edit");
            e.get_Node().BeginEdit();
        }
        this.treeView1.set_LabelEdit(false);
    }
} //treeView1_AfterLabelEdit

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft