String.IndexOfAny メソッド (Char[])
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim anyOf As Char() Dim returnValue As Integer returnValue = instance.IndexOfAny(anyOf)
戻り値
anyOf 内の文字がこのインスタンスで最初に見つかった場所のインデックス位置。anyOf 内の文字が見つからなかった場合は -1。


このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

IndexOfAny メソッドを使用して、ユーザーが入力した文字列内の無効な文字をチェックする方法については、次のコード例を参照してください。
' 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
/* 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->X, e->Y ); } void menuItem1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { if ( mySelectedNode != nullptr && mySelectedNode->Parent != nullptr ) { treeView1->SelectedNode = mySelectedNode; treeView1->LabelEdit = true; if ( !mySelectedNode->IsEditing ) { mySelectedNode->BeginEdit(); } } else { MessageBox::Show( String::Concat( "No tree node selected or selected node is a root node.\n", "Editing of root nodes is not allowed." ), "Invalid selection" ); } } void treeView1_AfterLabelEdit( Object^ /*sender*/, System::Windows::Forms::NodeLabelEditEventArgs^ e ) { if ( e->Label != nullptr ) { if ( e->Label->Length > 0 ) { array<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( String::Concat( "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 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOfAny メソッド (Char[], Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim anyOf As Char() Dim startIndex As Integer Dim returnValue As Integer returnValue = instance.IndexOfAny(anyOf, startIndex)
戻り値
anyOf 内の文字がこのインスタンスで最初に見つかった場所のインデックス位置。anyOf 内の文字が見つからなかった場合は -1。


検索範囲は startIndex から文字列の末尾までです。
このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

部分文字列から、"is" を構成する任意の文字が出現するインデックス位置を検索するコード例を次に示します。
' Sample for String.IndexOfAny(Char[], Int32) Imports System Class Sample Public Shared Sub Main() Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-" Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456" Dim str As String = "Now is the time for all good men to come to the aid of their party." Dim start As Integer Dim at As Integer Dim target As String = "is" Dim anyOf As Char() = target.ToCharArray() start = str.Length / 2 Console.WriteLine() Console.WriteLine("Search for a character occurrence from position {0} to {1}.", _ start, str.Length - 1) Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str) Console.Write("A character in '{0}' occurs at position: ", target) at = str.IndexOfAny(anyOf, start) If at > - 1 Then Console.Write(at) Else Console.Write("(not found)") End If Console.WriteLine() End Sub 'Main End Class 'Sample ' ' 'Search for a character occurrence from position 33 to 66. '0----+----1----+----2----+----3----+----4----+----5----+----6----+- '0123456789012345678901234567890123456789012345678901234567890123456 'Now is the time for all good men to come to the aid of their party. ' 'A character in 'is' occurs at position: 49 '
// Sample for String.IndexOfAny(Char[], Int32) using System; class Sample { public static void Main() { string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; string br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; string str = "Now is the time for all good men to come to the aid of their party."; int start; int at; string target = "is"; char[] anyOf = target.ToCharArray(); start = str.Length/2; Console.WriteLine(); Console.WriteLine("The first character occurrence from position {0} to {1}.", start, str.Length-1); Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str); Console.Write("A character in '{0}' occurs at position: ", target); at = str.IndexOfAny(anyOf, start); if (at > -1) Console.Write(at); else Console.Write("(not found)"); Console.WriteLine(); } } /* The first character occurrence from position 33 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. A character in 'is' occurs at position: 49 */
// Sample for String::IndexOfAny(Char[], Int32) using namespace System; int main() { String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; String^ str = "Now is the time for all good men to come to the aid of their party."; int start; int at; String^ target = "is"; array<Char>^anyOf = target->ToCharArray(); start = str->Length / 2; Console::WriteLine(); Console::WriteLine( "The first character occurrence from position {0} to {1}.", start, str->Length - 1 ); Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str ); Console::Write( "A character in '{0}' occurs at position: ", target ); at = str->IndexOfAny( anyOf, start ); if ( at > -1 ) Console::Write( at ); else Console::Write( "(not found)" ); Console::WriteLine(); } /* The first character occurrence from position 33 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. A character in 'is' occurs at position: 49 */
// Sample for String.IndexOfAny(Char[], Int32) import System.*; class Sample { public static void main(String[] args) { String br1 = "0----+----1----+----2----+----3----+----4----+----5----" + "+----6----+-"; String br2 = "012345678901234567890123456789012345678901234567890123" + "4567890123456"; String str = "Now is the time for all good men to come to the aid of " + "their party."; int start; int at; String target = "is"; char anyOf[] = target.ToCharArray(); start = str.get_Length() / 2; Console.WriteLine(); Console.WriteLine("The first character occurrence from position {0} to " + "{1}.", (Int32)start, (Int32)(str.get_Length() - 1)); Console.Write("{1}{0}", Environment.get_NewLine(), br1); Console.Write("{1}{0}", Environment.get_NewLine(), br2); Console.WriteLine("{1}{0}", Environment.get_NewLine(), str); Console.Write("A character in '{0}' occurs at position: ", target); at = str.IndexOfAny(anyOf, start); if (at > -1) { Console.Write(at); } else { Console.Write("(not found)"); } Console.WriteLine(); } //main } //Sample /* The first character occurrence from position 33 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. A character in 'is' occurs at position: 49 */

Windows 98, Windows 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOfAny メソッド (Char[], Int32, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Function IndexOfAny ( _ anyOf As Char(), _ startIndex As Integer, _ count As Integer _ ) As Integer
Dim instance As String Dim anyOf As Char() Dim startIndex As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.IndexOfAny(anyOf, startIndex, count)
戻り値
anyOf 内の文字がこのインスタンスで最初に見つかった場所のインデックス位置。anyOf 内の文字が見つからなかった場合は -1。


startIndex から startIndex + count -1 番目の位置まで検索が実行されましたが、startIndex + count の文字は検出されませんでした。
このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

部分文字列から、"aid" を構成する任意の文字が出現するインデックス位置を検索するコード例を次に示します。
' Sample for String.IndexOfAny(Char[], Int32, Int32) Imports System Class Sample Public Shared Sub Main() Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-" Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456" Dim str As String = "Now is the time for all good men to come to the aid of their party." Dim start As Integer Dim at As Integer Dim count As Integer Dim target As String = "aid" Dim anyOf As Char() = target.ToCharArray() start =(str.Length - 1) / 3 count =(str.Length - 1) / 4 Console.WriteLine() Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count) Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str) Console.Write("A character in '{0}' occurs at position: ", target) at = str.IndexOfAny(anyOf, start, count) If at > - 1 Then Console.Write(at) Else Console.Write("(not found)") End If Console.WriteLine() End Sub 'Main End Class 'Sample ' 'The first character occurrence from position 22 for 16 characters. '0----+----1----+----2----+----3----+----4----+----5----+----6----+- '0123456789012345678901234567890123456789012345678901234567890123456 'Now is the time for all good men to come to the aid of their party. ' 'A character in 'aid' occurs at position: 27 '
// Sample for String.IndexOfAny(Char[], Int32, Int32) using System; class Sample { public static void Main() { string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; string br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; string str = "Now is the time for all good men to come to the aid of their party."; int start; int at; int count; string target = "aid"; char[] anyOf = target.ToCharArray(); start = (str.Length-1)/3; count = (str.Length-1)/4; Console.WriteLine(); Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count); Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str); Console.Write("A character in '{0}' occurs at position: ", target); at = str.IndexOfAny(anyOf, start, count); if (at > -1) Console.Write(at); else Console.Write("(not found)"); Console.WriteLine(); } } /* The first character occurrence from position 22 for 16 characters. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. A character in 'aid' occurs at position: 27 */
// Sample for String::IndexOfAny(Char[], Int32, Int32) using namespace System; int main() { String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; String^ str = "Now is the time for all good men to come to the aid of their party."; int start; int at; int count; String^ target = "aid"; array<Char>^anyOf = target->ToCharArray(); start = (str->Length - 1) / 3; count = (str->Length - 1) / 4; Console::WriteLine(); Console::WriteLine( "The first character occurrence from position {0} for {1} characters.", start, count ); Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str ); Console::Write( "A character in '{0}' occurs at position: ", target ); at = str->IndexOfAny( anyOf, start, count ); if ( at > -1 ) Console::Write( at ); else Console::Write( "(not found)" ); Console::WriteLine(); } /* The first character occurrence from position 22 for 16 characters. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. A character in 'aid' occurs at position: 27 */
// Sample for String.IndexOfAny(Char[], Int32, Int32) import System.*; class Sample { public static void main(String[] args) { String br1 = "0----+----1----+----2----+----3----+----4----+----5----+" + "----6----+-"; String br2 = "01234567890123456789012345678901234567890123456789012345" + "67890123456"; String str = "Now is the time for all good men to come to the aid of " + "their party."; int start; int at; int count; String target = "aid"; char anyOf[] = target.ToCharArray(); start = (str.get_Length() - 1) / 3; count = (str.get_Length() - 1) / 4; Console.WriteLine(); Console.WriteLine("The first character occurrence from position {0} " + "for {1} characters.", (Int32)start, (Int32)count); Console.Write("{1}{0}", Environment.get_NewLine(), br1); Console.Write("{1}{0}", Environment.get_NewLine(), br2); Console.WriteLine("{1}{0}", Environment.get_NewLine(), str); Console.Write("A character in '{0}' occurs at position: ", target); at = str.IndexOfAny(anyOf, start, count); if (at > -1) { Console.Write(at); } else { Console.Write("(not found)"); } Console.WriteLine(); } //main } //Sample /* The first character occurrence from position 22 for 16 characters. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. A character in 'aid' occurs at position: 27 */

Windows 98, Windows 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOfAny メソッド

名前 | 説明 |
---|---|
String.IndexOfAny (Char[]) | Unicode 文字の指定した配列内にある文字がこのインスタンスで最初に見つかった位置のインデックスをレポートします。 .NET Compact Framework によってサポートされています。 |
String.IndexOfAny (Char[], Int32) | Unicode 文字の指定した配列内にある文字がこのインスタンスで最初に見つかった位置のインデックスをレポートします。検索は、指定した文字位置から開始されます。 .NET Compact Framework によってサポートされています。 |
String.IndexOfAny (Char[], Int32, Int32) | Unicode 文字の指定した配列内にある文字がこのインスタンスで最初に見つかった位置のインデックスをレポートします。検索は指定した文字位置から開始され、指定した数の文字位置が検査されます。 .NET Compact Framework によってサポートされています。 |

- String.IndexOfAnyのページへのリンク