共用方式為


String.IndexOfAny 方法

定義

報告指定 Unicode 字元陣列中的任何字元於這個執行個體中第一個符合項目的索引。 如果在此執行個體中找不到陣列中的字元,此方法會傳回 -1。

多載

IndexOfAny(Char[])

報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。

IndexOfAny(Char[], Int32)

報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。 搜尋從指定的字元位置開始。

IndexOfAny(Char[], Int32, Int32)

報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。 搜尋從指定的字元位置開始,並檢視指定數目的字元位置。

IndexOfAny(Char[])

來源:
String.Searching.cs
來源:
String.Searching.cs
來源:
String.Searching.cs

報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。

public:
 int IndexOfAny(cli::array <char> ^ anyOf);
public int IndexOfAny (char[] anyOf);
member this.IndexOfAny : char[] -> int
Public Function IndexOfAny (anyOf As Char()) As Integer

參數

anyOf
Char[]

Unicode 字元陣列,含有一或多個要搜尋的字元。

傳回

在此執行個體中,anyOf 中的任何字元第一次出現的所在索引位置 (以零為起始),如果找不到 anyOf 中的字元,則為 -1。

例外狀況

anyOfnull

範例

下列範例會尋找字串中的第一個 vowel。

using System;

public class Example
{
   public static void Main()
   {
      char[] chars = { 'a', 'e', 'i', 'o', 'u', 'y', 
                       'A', 'E', 'I', 'O', 'U', 'Y' };
      String s = "The long and winding road...";
      Console.WriteLine("The first vowel in \n   {0}\nis found at position {1}", 
                        s, s.IndexOfAny(chars) + 1);                         
   }
}
// The example displays the following output:
//       The first vowel in
//          The long and winding road...
//       is found at position 3
let chars = [| 'a'; 'e'; 'i'; 'o'; 'u'; 'y'
               'A'; 'E'; 'I'; 'O'; 'U'; 'Y' |]
let s = "The long and winding road..."
printfn $"The first vowel in \n   {s}\nis found at position {s.IndexOfAny chars + 1}"
// The example displays the following output:
//       The first vowel in
//          The long and winding road...
//       is found at position 3
Module Example
   Public Sub Main()
      Dim chars() As Char = { "a"c, "e"c, "i"c, "o"c, "u"c, "y"c, 
                              "A"c, "E"c, "I"c, "O"c, "U"c, "Y"c }
      Dim s As String = "The long and winding road..."
      Console.WriteLine("The first vowel in {2}   {0}{2}is found at position {1}", 
                        s, s.IndexOfAny(chars) + 1, vbCrLf)                         
   End Sub
End Module
' The example displays the following output:
'       The first vowel in
'          The long and winding road...
'       is found at position 3

備註

索引編號從零開始。

anyOf搜尋會區分大小寫。 如果 anyOf 是空陣列,則方法會在字串開頭找到相符專案 (,也就是索引零) 。

這個方法會執行不區分文化特性的序數 (不區分文化特性) 搜尋,其中字元只有在 Unicode 純量值相同時,才會被視為相當於另一個字元。 若要執行區分文化特性的搜尋,請使用 CompareInfo.IndexOf 方法,其中代表預先編譯字元的 Unicode 純量值,例如 ligature 「Æ」 (U+00C6) ,可能會視為相當於正確序列中任何出現的字元元件,例如 「AE」 (U+0041、U+0045) ,視文化特性而定。

另請參閱

適用於

IndexOfAny(Char[], Int32)

來源:
String.Searching.cs
來源:
String.Searching.cs
來源:
String.Searching.cs

報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。 搜尋從指定的字元位置開始。

public:
 int IndexOfAny(cli::array <char> ^ anyOf, int startIndex);
public int IndexOfAny (char[] anyOf, int startIndex);
member this.IndexOfAny : char[] * int -> int
Public Function IndexOfAny (anyOf As Char(), startIndex As Integer) As Integer

參數

anyOf
Char[]

Unicode 字元陣列,含有一或多個要搜尋的字元。

startIndex
Int32

搜尋開始位置。

傳回

在此執行個體中,anyOf 中的任何字元第一次出現的所在索引位置 (以零為起始),如果找不到 anyOf 中的字元,則為 -1。

例外狀況

anyOfnull

startIndex 為負。

-或-

startIndex 大於這個執行個體的字元數。

範例

下列範例會在另一個字串的子字串內,尋找字串 「is」 中出現之任何字元的索引。

// 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)
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)
open System

let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "is"
let anyOf = target.ToCharArray()

let start = str.Length/2
printfn $"\nThe first character occurrence from position {start} to {str.Length - 1}."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "

let at = str.IndexOfAny(anyOf, start)
if at > -1 then
    printfn $"{at}"
else
    printfn "(not found)"
(*

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)
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
End Class
'
'
'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
'

備註

索引編號從零開始。 參數 startIndex 的範圍可以從 0 到小於字串實例的長度。

搜尋範圍從 startIndex 到字串結尾。

anyOf搜尋會區分大小寫。

這個方法會執行不區分文化特性的序數 (不區分文化特性) 搜尋,其中字元只有在 Unicode 純量值相同時才視為另一個字元。 若要執行區分文化特性的搜尋,請使用 CompareInfo.IndexOf 方法,其中代表預先編譯字元的 Unicode 純量值,例如 ligature 「Æ」 (U+00C6) ,可能會視為相當於正確序列中任何出現的字元元件,例如 「AE」 (U+0041、U+0045) ,視文化特性而定。

另請參閱

適用於

IndexOfAny(Char[], Int32, Int32)

來源:
String.Searching.cs
來源:
String.Searching.cs
來源:
String.Searching.cs

報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。 搜尋從指定的字元位置開始,並檢視指定數目的字元位置。

public:
 int IndexOfAny(cli::array <char> ^ anyOf, int startIndex, int count);
public int IndexOfAny (char[] anyOf, int startIndex, int count);
member this.IndexOfAny : char[] * int * int -> int
Public Function IndexOfAny (anyOf As Char(), startIndex As Integer, count As Integer) As Integer

參數

anyOf
Char[]

Unicode 字元陣列,含有一或多個要搜尋的字元。

startIndex
Int32

搜尋開始位置。

count
Int32

要檢視的字元位置數目。

傳回

在此執行個體中,anyOf 中的任何字元第一次出現的所在索引位置 (以零為起始),如果找不到 anyOf 中的字元,則為 -1。

例外狀況

anyOfnull

countstartIndex 為負。

-或-

count + startIndex 大於這個執行個體的字元數。

範例

下列範例會尋找另一個字串子字串內任何字串 「aid」 字元出現次數的索引。

// 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)
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)
open System

let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "aid"
let anyOf = target.ToCharArray()

let start = (str.Length - 1) / 3
let count = (str.Length - 1) / 4
printfn $"\nThe first character occurrence from position {start} for {count} characters."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "

let at = str.IndexOfAny(anyOf, start, count)
if at > -1 then
    printfn $"{at}"
else
    printfn "(not found)"
(*

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)
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
End Class
'
'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
'

備註

搜尋會從 開始 startIndex ,並繼續 - startIndex + count 1。 位於 的字元 startIndex + count 未包含在搜尋中。

索引編號從零開始。 參數 startIndex 的範圍可以從 0 到小於字串實例的長度。

anyOf搜尋會區分大小寫。

這個方法會執行不區分文化特性的序數 (不區分文化特性) 搜尋,其中字元只有在 Unicode 純量值相同時才視為另一個字元。 若要執行區分文化特性的搜尋,請使用 CompareInfo.IndexOf 方法,其中代表預先編譯字元的 Unicode 純量值,例如 ligature 「Æ」 (U+00C6) ,可能會視為相當於正確序列中任何出現的字元元件,例如 「AE」 (U+0041、U+0045) ,視文化特性而定。

另請參閱

適用於