本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

String.IndexOfAny 方法 (Char[])

 

发布日期: 2016年7月

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。

命名空间:   System
程序集:  mscorlib(位于 mscorlib.dll)

public int IndexOfAny(
	char[] anyOf
)

参数

anyOf
Type: System.Char[]

Unicode 字符数组,包含一个或多个要查找的字符。

返回值

Type: System.Int32

在此实例中第一次找到 anyOf 中的任意字符的索引位置(从零开始);如果未找到 anyOf 中的字符,则为 -1。

Exception Condition
ArgumentNullException

anyOfnull

Index numbering starts from zero.

The search for anyOf is case-sensitive. If anyOf is an empty array, the method finds a match at the beginning of the string (that is, at index zero).

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. To perform a culture-sensitive search, use the M:System.Globalization.CompareInfo.IndexOf(System.String,System.Char) 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.

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

通用 Windows 平台
自 8 起可用
.NET Framework
自 1.1 起可用
可移植类库
可移植 .NET 平台 中受支持
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用
返回页首
显示: