导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

CompareInfo.IndexOf 方法 (String, Char, Int32, Int32)

2013/12/13

搜索指定的字符,并返回源字符串内从指定的索引位置开始、包含所指定元素数的部分中第一个匹配项的从零开始的索引。

Namespace:  System.Globalization
程序集:  mscorlib(位于 mscorlib.dll 中)

public virtual int IndexOf(
	string source,
	char value,
	int startIndex,
	int count
)

参数

source
类型: System.String
要搜索的字符串。
value
类型: System.Char
要在 source 中定位的字符。
startIndex
类型: System.Int32
搜索的从零开始的起始索引。
count
类型: System.Int32
要搜索的部分中的元素数。

返回值

类型: System.Int32
如果在 source 的从 startIndex 开始、包含 count 所指定的元素数的部分中,找到 value 的第一个匹配项,则为该项的从零开始的索引;否则为 -1。

异常条件
ArgumentNullException

sourcenull

ArgumentOutOfRangeException

startIndex 超出了 source 的有效索引范围。

- 或 -

count 小于零。

- 或 -

startIndexcount 指定的不是 source 中的有效部分。

在源字符串中向前搜索,从 startIndex 开始,到 startIndex+count- 1 处结束。

此重载执行区分区域性的搜索。如果该字符是一个表示复合字符的 Unicode 值,例如连字“Æ”(U+00C6),则根据区域性的不同,它可能被视为等效于其以正确顺序排列的任何组成部分,例如“AE”(U+0041、U+0045)。在顺序(不区分区域性)搜索中,只有当两个字符的 Unicode 值相同时,才会将它们视为相等。若要执行顺序搜索,应用程序应使用接受 CompareOptions 值作为参数的重载之一,同时使用 Ordinal 值。

下面的示例确定字符串的一部分中的字符或子字符串的第一个和最后一个匹配项的索引。


using System;
using System.Globalization;

public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Creates CompareInfo for the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // iS is the starting index of the substring.
      int iS = 8;
      // iL is the length of the substring.
      int iL = 18;
      // myT1 and myT2 are the strings used for padding.
      String myT1 = new String('-', iS);
      String myT2;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      myT2 = new String('-', myStr.Length - iS - iL);
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Original      : {0}", myStr) + "\n";
      outputBlock.Text += String.Format("No options    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", iS, iL), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL));
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", iS, iL), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL));
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL));
      outputBlock.Text += String.Format("Ordinal       : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal));
      outputBlock.Text += String.Format("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL, CompareOptions.IgnoreCase));

      // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
      myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
      myT2 = new String('-', myStr.Length - iS - iL);
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Original      : {0}", myStr) + "\n";
      outputBlock.Text += String.Format("No options    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL));
      outputBlock.Text += String.Format("Ordinal       : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal));
      outputBlock.Text += String.Format("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL, CompareOptions.IgnoreCase));

   }

   public static void PrintMarker(System.Windows.Controls.TextBlock outputBlock, String Prefix, int First, int Last)
   {

      // Determines the size of the array to create.
      int mySize;
      if (Last > First)
         mySize = Last;
      else
         mySize = First;

      if (mySize > -1)
      {

         // Creates an array of Char to hold the markers.
         Char[] myCharArr = new Char[mySize + 1];

         // Inserts the appropriate markers.
         if (First > -1)
            myCharArr[First] = 'f';
         if (Last > -1)
            myCharArr[Last] = 'l';
         if (First == Last)
            myCharArr[First] = 'b';

         // Displays the array of Char as a String.
         outputBlock.Text += String.Format("{0}{1}", Prefix, new String(myCharArr)) + "\n";

      }
      else
         outputBlock.Text += Prefix + "\n";

   }

}


/*
This code produces the following output.

Original      : Is AE or ae the same as Æ or æ?
No options    : -------- ae the same as Æ -----
           AE :                         b
           ae :          b
            Æ :                         b
            æ :          b
Ordinal       : -------- ae the same as Æ -----
           AE :
           ae :          b
            Æ :                         b
            æ :
IgnoreCase    : -------- ae the same as Æ -----
           AE :          f              l
           ae :          f              l
            Æ :          f              l
            æ :          f              l

Original      : Is U" or u" the same as Ü or ü?
No options    : -------- u" the same as Ü -----
           U" :                         b
           u" :          b
            Ü :                         b
            ü :          b
Ordinal       : -------- u" the same as Ü -----
           U" :
           u" :          b
            Ü :                         b
            ü :
IgnoreCase    : -------- u" the same as Ü -----
           U" :          f              l
           u" :          f              l
            Ü :          f              l
            ü :          f              l

*/


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示:
© 2014 Microsoft