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
报告指定 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
。
示例
以下示例查找字符串中的第一个元音。
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 标量值(如连字“Æ” (U+00C6) )可能被视为等效于字符组件按正确顺序出现的任何事件,例如“AE” (U+0041、U+0045) ,具体取决于区域性。
另请参阅
适用于
IndexOfAny(Char[], Int32)
- 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”的任何字符出现的索引。
// 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 到比字符串实例的长度少 1。
搜索范围从 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
报告指定 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”的任何字符出现的索引。
// 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 到比字符串实例的长度少 1。
搜索区分 anyOf
大小写。
此方法执行不区分区域性的序号 () 搜索,其中仅当一个字符的 Unicode 标量值相同时,才将字符视为等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 方法,其中表示预分解字符的 Unicode 标量值(如连字“Æ” (U+00C6) )可能被视为等效于字符组件按正确顺序出现的任何事件,例如“AE” (U+0041、U+0045) ,具体取决于区域性。