String.IndexOfAny 方法
定義
報告指定 Unicode 字元陣列中的任何字元於這個執行個體中第一個符合項目的索引。Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters. 如果在此執行個體中找不到陣列中的字元,此方法會傳回 -1。The method returns -1 if the characters in the array are not found in this instance.
多載
IndexOfAny(Char[]) |
報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. |
IndexOfAny(Char[], Int32) |
報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 搜尋從指定的字元位置開始。The search starts at a specified character position. |
IndexOfAny(Char[], Int32, Int32) |
報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 搜尋從指定的字元位置開始,並檢視指定數目的字元位置。The search starts at a specified character position and examines a specified number of character positions. |
IndexOfAny(Char[])
報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
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 字元陣列,含有一或多個要搜尋的字元。A Unicode character array containing one or more characters to seek.
傳回
在此執行個體中,anyOf
中的任何字元第一次出現的所在索引位置 (以零為起始),如果找不到 anyOf
中的字元,則為 -1。The 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.
例外狀況
anyOf
為 null
。anyOf
is null
.
範例
下列範例會尋找字串中的第一個母音。The following example finds the first vowel in a string.
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
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
備註
索引編號從零開始。Index numbering starts from zero.
搜尋會區分 anyOf
大小寫。The search for anyOf
is case-sensitive. 如果 anyOf
是空陣列,方法會在字串的開頭找到相符的 (也就是在索引零) 。If anyOf
is an empty array, the method finds a match at the beginning of the string (that is, at index zero).
這個方法會執行不區分文化特性) 搜尋的序數 (,其中只有當 Unicode 純量值相同時,才會將字元視為等於另一個字元。This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. 若要執行區分文化特性的搜尋,請使用 CompareInfo.IndexOf 方法,其中代表 precomposed 字元的 Unicode 純量值(例如連字號 "Æ" (U + 00C6) )可能會被視為等於正確順序中任何相符字元的元件,例如 "AE" (U + 0041,U + 0045) ,視文化特性而定。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.
另請參閱
適用於
IndexOfAny(Char[], Int32)
報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 搜尋從指定的字元位置開始。The search starts at a specified character position.
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 字元陣列,含有一或多個要搜尋的字元。A Unicode character array containing one or more characters to seek.
- startIndex
- Int32
搜尋開始位置。The search starting position.
傳回
在此執行個體中,anyOf
中的任何字元第一次出現的所在索引位置 (以零為起始),如果找不到 anyOf
中的字元,則為 -1。The 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.
例外狀況
anyOf
為 null
。anyOf
is null
.
startIndex
為負。startIndex
is negative.
-或--or-
startIndex
大於這個執行個體的字元數。startIndex
is greater than the number of characters in this instance.
範例
下列範例會在另一個字串的子字串內,尋找字串 "是" 的任何字元之出現位置的索引。The following example finds the index of the occurrence of any character of the string "is" within a substring of another string.
// 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)
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
'
備註
索引編號從零開始。Index numbering starts from zero. startIndex
參數的範圍可以從0到1,小於字串實例的長度。The startIndex
parameter can range from 0 to one less than the length of the string instance.
搜尋範圍從 startIndex
到字串的結尾。The search ranges from startIndex
to the end of the string.
搜尋會區分 anyOf
大小寫。The search for anyOf
is case-sensitive.
這個方法會執行不區分文化特性) 搜尋的序數 (,其中只有當 Unicode 純量值相同時,才會將字元視為等於另一個字元。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. 若要執行區分文化特性的搜尋,請使用 CompareInfo.IndexOf 方法,其中代表 precomposed 字元的 Unicode 純量值(例如連字號 "Æ" (U + 00C6) )可能會被視為等於正確順序中任何相符字元的元件,例如 "AE" (U + 0041,U + 0045) ,視文化特性而定。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.
另請參閱
適用於
IndexOfAny(Char[], Int32, Int32)
報告指定的 Unicode 字元陣列中,任何字元在這個執行個體中第一次出現時的所在索引 (以零為起始)。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 搜尋從指定的字元位置開始,並檢視指定數目的字元位置。The search starts at a specified character position and examines a specified number of character positions.
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 字元陣列,含有一或多個要搜尋的字元。A Unicode character array containing one or more characters to seek.
- startIndex
- Int32
搜尋開始位置。The search starting position.
- count
- Int32
要檢視的字元位置數目。The number of character positions to examine.
傳回
在此執行個體中,anyOf
中的任何字元第一次出現的所在索引位置 (以零為起始),如果找不到 anyOf
中的字元,則為 -1。The 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.
例外狀況
anyOf
為 null
。anyOf
is null
.
count
或 startIndex
為負。count
or startIndex
is negative.
-或--or-
count
+ startIndex
大於這個執行個體的字元數。count
+ startIndex
is greater than the number of characters in this instance.
範例
下列範例會在另一個字串的子字串內,尋找字串 "輔助" 的任何字元之出現位置的索引。The following example finds the index of the occurrence of any character of the string "aid" within a substring of another string.
// 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)
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。The search begins at startIndex
and continues to startIndex
+ count
-1. 中的字元 startIndex
+ count
不包含在搜尋中。The character at startIndex
+ count
is not included in the search.
索引編號從零開始。Index numbering starts from zero. startIndex
參數的範圍可以從0到1,小於字串實例的長度。The startIndex
parameter can range from 0 to one less than the length of the string instance.
搜尋會區分 anyOf
大小寫。The search for anyOf
is case-sensitive.
這個方法會執行不區分文化特性) 搜尋的序數 (,其中只有當 Unicode 純量值相同時,才會將字元視為等於另一個字元。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. 若要執行區分文化特性的搜尋,請使用 CompareInfo.IndexOf 方法,其中代表 precomposed 字元的 Unicode 純量值(例如連字號 "Æ" (U + 00C6) )可能會被視為等於正確順序中任何相符字元的元件,例如 "AE" (U + 0041,U + 0045) ,視文化特性而定。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.