String.IndexOfAny 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
报告指定 Unicode 字符数组中任何字符在此实例中出现的第一个匹配项的索引。 如果在此实例中找不到数组中的字符,该方法将返回 -1。
重载
| 名称 | 说明 |
|---|---|
| IndexOfAny(Char[]) |
报告指定 Unicode 字符数组中任何字符的此实例中第一个匹配项的从零开始的索引。 |
| IndexOfAny(Char[], Int32) |
报告指定 Unicode 字符数组中任何字符的此实例中第一个匹配项的从零开始的索引。 搜索从指定的字符位置开始。 |
| IndexOfAny(Char[], Int32, Int32) |
报告指定 Unicode 字符数组中任何字符的此实例中第一个匹配项的从零开始的索引。 搜索从指定的字符位置开始,并检查指定数量的字符位置。 |
IndexOfAny(Char[])
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- 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。
例外
anyOf 是 null。
示例
以下示例查找字符串中的第一个元音。
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
'{s}'
is found at index {s.IndexOfAny(chars)}
""");
// The example displays the following output:
// The first vowel in
// 'The long and winding road...'
// is found at index 2
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 index {s.IndexOfAny chars}"
// The example displays the following output:
// The first vowel in
// The long and winding road...
// is found at index 2
Module Example1
Public Sub Run()
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 index {1}",
s, s.IndexOfAny(chars), vbCrLf)
End Sub
End Module
' The example displays the following output:
' The first vowel in
' The long and winding road...
' is found at index 2
注解
索引编号从零开始。
搜索 anyOf 区分大小写。 如果 anyOf 为空数组,则该方法返回 -1。
此方法执行序号(区域性不区分区域性)搜索,其中字符仅当 Unicode 标量值相同时才被视为等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 该方法,其中表示预编译字符的 Unicode 标量值(如连字“Æ”(U+00C6)可能被视为等效于正确序列中字符组件的任何匹配项,例如“AE”(U+0041、U+0045),具体取决于区域性。
另请参阅
适用于
IndexOfAny(Char[], Int32)
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- 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。
例外
anyOf 是 null。
示例
以下示例查找另一个字符串的子字符串中字符串“is”出现的任何字符的索引。
string br1 = "0----+----1----+----2----+----3" +
"----+----4----+----5----+----6----+-";
string br2 = "012345678901234567890123456789" +
"0123456789012345678901234567890123456";
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 {start} to {str.Length - 1}:");
Console.WriteLine($"""
{Environment.NewLine}{br1}{Environment.NewLine}
{br2}{Environment.NewLine}{str}{Environment.NewLine}
""");
Console.Write($"A character in '{target}' occurs at position: ");
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 Example2
Public Shared Sub Run()
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 到小于字符串实例长度的 0。
搜索范围从 startIndex 字符串的末尾开始。
搜索 anyOf 区分大小写。
此方法执行序号(区域性不区分区域性)搜索,其中字符仅当 Unicode 标量值相同时才被视为等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 该方法,其中表示预编译字符的 Unicode 标量值(如连字“Æ”(U+00C6)可能被视为等效于正确序列中字符组件的任何匹配项,例如“AE”(U+0041、U+0045),具体取决于区域性。
另请参阅
适用于
IndexOfAny(Char[], Int32, Int32)
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- 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。
例外
anyOf 是 null。
示例
以下示例查找另一个字符串子字符串中字符串“aid”的任何字符出现的索引。
string br1 = "0----+----1----+----2----+----3----" +
"+----4----+----5----+----6----+-";
string br2 = "012345678901234567890123456789" +
"0123456789012345678901234567890123456";
string str = "Now is the time for all good men " +
"to come to the aid of their party.";
string target = "aid";
char[] anyOf = target.ToCharArray();
int start = (str.Length - 1) / 3;
int count = (str.Length - 1) / 4;
Console.WriteLine();
Console.WriteLine("The first character occurrence from " +
$"position {start} for {count} characters:");
Console.WriteLine($"""
{Environment.NewLine}{br1}{Environment.NewLine}{br2}
{Environment.NewLine}{str}{Environment.NewLine}
""");
Console.Write($"A character in '{target}' occurs at position: ");
int 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 Example3
Public Shared Sub Run()
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 到小于字符串实例长度的 0。
搜索 anyOf 区分大小写。
此方法执行序号(区域性不区分区域性)搜索,其中字符仅当 Unicode 标量值相同时才被视为等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 该方法,其中表示预编译字符的 Unicode 标量值(如连字“Æ”(U+00C6)可能被视为等效于正确序列中字符组件的任何匹配项,例如“AE”(U+0041、U+0045),具体取决于区域性。