Export (0) Print
Expand All

String.IndexOfAny Method (Char[])

.NET Framework 1.1

Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters.

[Visual Basic]
Overloads Public Function IndexOfAny( _
   ByVal anyOf() As Char _
) As Integer
[C#]
public int IndexOfAny(
 char[] anyOf
);
[C++]
public: int IndexOfAny(
 __wchar_t anyOf __gc[]
);
[JScript]
public function IndexOfAny(
   anyOf : Char[]
) : int;

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.

Exceptions

Exception Type Condition
ArgumentNullException anyOf is a null reference (Nothing in Visual Basic).

Remarks

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.

Example

[Visual Basic, C#, C++] The following code example uses the IndexOfAny method to check for invalid characters in a user entered string.

[Visual Basic] 
' Get the tree node under the mouse pointer and
' save it in the mySelectedNode variable. 
Private Sub treeView1_MouseDown(sender As Object, _
  e As System.Windows.Forms.MouseEventArgs)
        
   mySelectedNode = treeView1.GetNodeAt(e.X, e.Y)
End Sub    
    
Private Sub menuItem1_Click(sender As Object, e As System.EventArgs)
   If Not (mySelectedNode Is Nothing) And _
     Not (mySelectedNode.Parent Is Nothing) Then
      treeView1.SelectedNode = mySelectedNode
      treeView1.LabelEdit = True
      If Not mySelectedNode.IsEditing Then
         mySelectedNode.BeginEdit()
      End If
   Else
      MessageBox.Show("No tree node selected or selected node is a root node." & _
        Microsoft.VisualBasic.ControlChars.Cr & _
        "Editing of root nodes is not allowed.", "Invalid selection")
   End If
End Sub    
    
Private Sub treeView1_AfterLabelEdit(sender As Object, _
  e As System.Windows.Forms.NodeLabelEditEventArgs)
   If Not (e.Label Is Nothing) Then
      If e.Label.Length > 0 Then
         If e.Label.IndexOfAny(New Char() {"@"c, "."c, ","c, "!"c}) = -1 Then
            ' 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." & _
              Microsoft.VisualBasic.ControlChars.Cr & _
              "The invalid characters are: '@','.', ',', '!'", _
              "Node Label Edit")
            e.Node.BeginEdit()
         End If
      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." & _
           Microsoft.VisualBasic.ControlChars.Cr & _
           "The label cannot be blank", "Node Label Edit")
           e.Node.BeginEdit()
      End If
         Me.treeView1.LabelEdit = False
   End If
End Sub 

[C#] 
/* 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;
   }
}

[C++] 
/* 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);
}

void menuItem1_Click(Object* /*sender*/, System::EventArgs* /*e*/)
{
   if (mySelectedNode != 0 && mySelectedNode->Parent != 0)
   {
      treeView1->SelectedNode = mySelectedNode;
      treeView1->LabelEdit = true;
      if(!mySelectedNode->IsEditing)
      {
         mySelectedNode->BeginEdit();
      }
   }
   else
   {
      MessageBox::Show(S"No tree node selected or selected node is a root node.\nEditing of root nodes is not allowed.", S"Invalid selection");
   }
}

void treeView1_AfterLabelEdit(Object* /*sender*/, 
         System::Windows::Forms::NodeLabelEditEventArgs* e)
{
   if (e->Label != 0)
   {
     if(e->Label->Length > 0)
     {

        Char temp0 [] = {'@', '.', ',', '!'};
        if (e->Label->IndexOfAny(temp0) == -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(S"Invalid tree node label.\nThe invalid characters are: '@','.', ',', '!'", 
              S"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(S"Invalid tree node label.\nThe label cannot be blank", 
           S"Node Label Edit");
        e->Node->BeginEdit();
     }
     this->treeView1->LabelEdit = false;
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.IndexOfAny Overload List | Char | Int32 | Array | IndexOf | LastIndexOf | LastIndexOfAny

Show:
© 2014 Microsoft