資訊
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.IndexOf 方法 (String, StringComparison)

報告目前 String 物件中指定之字串第一次出現時的所在索引 (以零起始)。 參數會指定要用於指定字串的搜尋類型。

命名空間:  System
組件:  mscorlib (在 mscorlib.dll 中)

public int IndexOf(
	string value,
	StringComparison comparisonType
)

參數

value
類型:System.String
要尋找的字串。
comparisonType
類型:System.StringComparison
其中一個列舉值,指定搜尋規則。

傳回值

類型:System.Int32
如果找到該字串,則為 value 的索引位置,如果沒有找到,則為 -1。 如果 valueEmpty,則傳回值是 0。

例外狀況條件
ArgumentNullException

valuenull

ArgumentException

comparisonType 不是有效的 System.StringComparison 值。

索引編號從零起始。

comparisonType 參數會指定使用目前或不因文化而異的特性、使用區大小寫或不區大小寫搜尋,以及使用字組或序數比較規則,以搜尋 value 參數。

呼叫者注意事項

字元集包含可忽略的字元,這些字元在執行語言或區分文化特性的比較時不列入考慮。 執行區分文化特性的搜尋 (也就是說,如果 comparisonType 不是 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase) 時,如果 value 包含可忽略的字元,則結果等於在已移除該字元的情況下搜尋。 如果 value 只包含一或多個可忽略的字元, IndexOf(String, StringComparison) 方法一定會傳回 0 (零) 表示比對在目前執行個體的開頭找到。

在下列範例中,使用 IndexOf(String, StringComparison) 方法來尋找在兩個字串的三子字串 (柔和連字號 (U+00AD), 「n」後面接著柔和連字號和「m」後面接著柔和連字號)。 只有其中一個字串包含選擇性連字號。 如果這個範例在 .NET Framework 4 或更新版本,執行,因為這個軟體的連字號為可忽略的字元,執行區分文化特性的搜尋傳回它將傳回相同的值,如果這個軟體的連字號在搜尋字串不包含。 循序搜尋,不過,成功尋找資料的彈性連字號並且報告它缺少第二個字串。


using System;

public class Example
{
   public static void Main()
   {
      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      Console.WriteLine("Culture-sensitive comparison:");
      // Use culture-sensitive comparison to find the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00AD", StringComparison.CurrentCulture));

      // Use culture-sensitive comparison to find the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf("\u00ADn", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00ADn", StringComparison.CurrentCulture));

      // Use culture-sensitive comparison to find the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf("\u00ADm", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00ADm", StringComparison.CurrentCulture));

      Console.WriteLine("Ordinal comparison:");
      // Use ordinal comparison to find the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD", StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf("\u00AD", StringComparison.Ordinal));

      // Use ordinal comparison to find the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf("\u00ADn", StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf("\u00ADn", StringComparison.Ordinal));

      // Use ordinal comparison to find the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf("\u00ADm", StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf("\u00ADm", StringComparison.Ordinal));
   }
}
// The example displays the following output:
//       Culture-sensitive comparison:
//       0
//       0
//       1
//       1
//       4
//       3
//       Ordinal comparison:
//       3
//       -1
//       -1
//       -1
//       3
//       -1


下列範例示範 IndexOf 方法的三個多載,該方法會使用 StringComparison 列舉型別的不同值,在另一個字串內尋找第一個符合的字串。


// This code example demonstrates the 
// System.String.IndexOf(String, ..., StringComparison) methods.

using System;
using System.Threading;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    string intro = "Find the first occurrence of a character using different " + 
                   "values of StringComparison.";
    string resultFmt = "Comparison: {0,-28} Location: {1,3}";

// Define a string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
    string CapitalAWithRing = "\u00c5"; 

// Define a string to search. 
// The result of combining the characters LATIN SMALL LETTER A and COMBINING 
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
    string cat = "A Cheshire c" + "\u0061\u030a" + "t";

    int loc = 0;
    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

// Clear the screen and display an introduction.
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because culture affects the result. For example, 
// try this code example with the "sv-SE" (Swedish-Sweden) culture.

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    Console.WriteLine("The current culture is \"{0}\" - {1}.", 
                       Thread.CurrentThread.CurrentCulture.Name,
                       Thread.CurrentThread.CurrentCulture.DisplayName);

// Display the string to search for and the string to search.
    Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", 
                       CapitalAWithRing, cat);
    Console.WriteLine();

// Note that in each of the following searches, we look for 
// LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains 
// LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates 
// the string was not found.
// Search using different values of StringComparison. Specify the start 
// index and count. 

    Console.WriteLine("Part 1: Start index and count are specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

// Search using different values of StringComparison. Specify the 
// start index. 
    Console.WriteLine("\nPart 2: Start index is specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.IndexOf(CapitalAWithRing, 0, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

// Search using different values of StringComparison. 
    Console.WriteLine("\nPart 3: Neither start index nor count is specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.IndexOf(CapitalAWithRing, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }
    }
}

/*
Note: This code example was executed on a console whose user interface 
culture is "en-US" (English-United States).

This code example produces the following results:

Find the first occurrence of a character using different values of StringComparison.
The current culture is "en-US" - English (United States).
Search for the string "Å" in the string "A Cheshire ca°t"

Part 1: Start index and count are specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 2: Start index is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 3: Neither start index nor count is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

*/


.NET Framework

支援版本:4.5.2、4.5.1、4.5、4、3.5、3.0、2.0

.NET Framework Client Profile

支援版本:4、3.5 SP1

可攜式類別庫

支援版本:可攜式類別庫

適用於 Windows 市集應用程式的 .NET

支援版本:Windows 8

適用於 Windows Phone 應用程式的 .NET

支援版本:Windows Phone 8、Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色,不支援 Itanium)

.NET Framework 並不支援各種平台的所有版本。如需支援版本的清單,請參閱.NET Framework 系統需求

社群新增項目

顯示:
© 2014 Microsoft