String.Substring 方法

定義

從這個執行個體擷取子字串。

這個成員是多載的。 如需這個成員的完整資訊,包含語法、使用方式和範例,請按一下多載清單中的名稱。

多載

Substring(Int32)

從這個執行個體擷取子字串。 子字串會在指定的字元位置開始並繼續到字串的結尾。

Substring(Int32, Int32)

從這個執行個體擷取子字串。 子字串起始於指定的字元位置,並且具有指定的長度。

Substring(Int32)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

從這個執行個體擷取子字串。 子字串會在指定的字元位置開始並繼續到字串的結尾。

C#
public string Substring (int startIndex);

參數

startIndex
Int32

這個執行個體中子字串之以零為起始的起始字元位置。

傳回

與這個執行個體中從 startIndex 開始之子字串相等的字串;如果 Empty 等於這個執行個體的長度,則為 startIndex

例外狀況

startIndex 小於零或大於此執行個體的長度。

範例

下列範例示範如何從字串取得子字串。

C#
string [] info = { "Name: Felica Walker", "Title: Mz.", 
                   "Age: 47", "Location: Paris", "Gender: F"};
int found = 0;

Console.WriteLine("The initial values in the array are:");
foreach (string s in info)
    Console.WriteLine(s);

Console.WriteLine("\nWe want to retrieve only the key information. That is:");        
foreach (string s in info) 
{
    found = s.IndexOf(": ");
    Console.WriteLine("   {0}", s.Substring(found + 2));
}

// The example displays the following output:
//       The initial values in the array are:
//       Name: Felica Walker
//       Title: Mz.
//       Age: 47
//       Location: Paris
//       Gender: F
//       
//       We want to retrieve only the key information. That is:
//          Felica Walker
//          Mz.
//          47
//          Paris
//          F

下列範例會 Substring 使用 方法來分隔以等號 () = 字元分隔的索引鍵/值組。

C#
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
                 "Title=Code Repository" };
foreach (var pair in pairs) 
{
    int position = pair.IndexOf("=");
    if (position < 0)
        continue;
    Console.WriteLine("Key: {0}, Value: '{1}'", 
                   pair.Substring(0, position),
                   pair.Substring(position + 1));
}                          

// The example displays the following output:
//     Key: Color1, Value: 'red'
//     Key: Color2, Value: 'green'
//     Key: Color3, Value: 'blue'
//     Key: Title, Value: 'Code Repository'

方法 IndexOf 可用來取得字串中等號字元的位置。 方法的呼叫 Substring(Int32, Int32) 會擷取索引鍵名稱,從字串中的第一個字元開始,並擴充方法呼叫 IndexOf 所傳回的字元數。 接著呼叫 Substring(Int32) 方法會擷取指派給索引鍵的值。 它會從等於字元以外的一個字元位置開始,並延伸至字串結尾。

備註

您可以呼叫 Substring(Int32) 方法,從字串擷取子字串,該字串從指定的字元位置開始,並在字串結尾結束。 起始字元位置是以零起始;換句話說,字串中的第一個字元位於索引 0,而不是索引 1。 若要擷取從指定字元位置開始的子字串,並在字串結尾之前結束,請呼叫 Substring(Int32, Int32) 方法。

備註

這個方法不會修改目前實例的值。 相反地,它會傳回以目前字串中位置開始 startIndex 的新字串。

若要擷取以特定字元或字元序列開頭的子字串,請呼叫 或 之類的 IndexOfIndexOf 方法來取得 的值 startIndex 。 第二個範例說明這一點;它會擷取索引鍵值,以在字元之後 = 開始一個字元位置。

如果 startIndex 等於零,方法會傳回原始字串未變更。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Substring(Int32, Int32)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

從這個執行個體擷取子字串。 子字串起始於指定的字元位置,並且具有指定的長度。

C#
public string Substring (int startIndex, int length);

參數

startIndex
Int32

這個執行個體中子字串之以零為起始的起始字元位置。

length
Int32

子字串中的字元數。

傳回

與長度為 length 且在這個執行個體中從 startIndex 開始之子字串相等的字串;如果 Empty 等於這個執行個體的長度且 startIndex 為零,則為 length

例外狀況

startIndex 加上 length 表示不在此執行個體中的位置。

-或-

startIndexlength 小於零。

範例

下列範例說明從字串擷取兩個字元的簡單呼叫 Substring(Int32, Int32) ,從第六個字元位置開始擷取兩個字元 (,也就是索引五) 。

C#
String value = "This is a string.";
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);

// The example displays the following output:
//       is

下列範例會使用 Substring(Int32, Int32) 下列三種案例中的 方法來隔離字串內的子字串。 在兩種情況下,子字串會用於比較,而第三個案例會擲回例外狀況,因為指定了不正確參數。

  • 它會擷取字串中第三個位置的單一字元, (索引 2) ,並將其與 「c」 進行比較。 此比較會傳 true 回 。

  • 它會從字串的第四個位置開始擷取零個字元, (索引 3) ,並將它傳遞至 IsNullOrEmpty 方法。 這會傳回 true,因為對 方法的 Substring 呼叫會傳 String.Empty 回 。

  • 它會嘗試從字串的第四個位置開始擷取一個字元。 因為該位置沒有字元,所以方法呼叫會 ArgumentOutOfRangeException 擲回例外狀況。

C#
string myString = "abc";
bool test1 = myString.Substring(2, 1).Equals("c"); // This is true.
Console.WriteLine(test1);
bool test2 = string.IsNullOrEmpty(myString.Substring(3, 0)); // This is true.
Console.WriteLine(test2);
try
{
   string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
   Console.WriteLine(str3);
}
catch (ArgumentOutOfRangeException e)
{
   Console.WriteLine(e.Message);
}

// The example displays the following output:
//       True
//       True
//       Index and length must refer to a location within the string.
//       Parameter name: length

下列範例會 Substring 使用 方法來分隔以等號 () = 字元分隔的索引鍵/值組。

C#
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
                 "Title=Code Repository" };
foreach (var pair in pairs) 
{
    int position = pair.IndexOf("=");
    if (position < 0)
        continue;
    Console.WriteLine("Key: {0}, Value: '{1}'", 
                   pair.Substring(0, position),
                   pair.Substring(position + 1));
}                          

// The example displays the following output:
//     Key: Color1, Value: 'red'
//     Key: Color2, Value: 'green'
//     Key: Color3, Value: 'blue'
//     Key: Title, Value: 'Code Repository'

方法 IndexOf 可用來取得字串中等號字元的位置。 方法的呼叫 Substring(Int32, Int32) 會擷取索引鍵名稱,從字串中的第一個字元開始,並擴充方法呼叫 IndexOf 所傳回的字元數。 接著呼叫 Substring(Int32) 方法會擷取指派給索引鍵的值。 它會從等於字元以外的一個字元位置開始,並延伸至字串結尾。

備註

您可以呼叫 Substring(Int32, Int32) 方法,從字串中擷取子字串,該字串從指定的字元位置開始,並在字串結尾之前結束。 起始字元位置是以零起始;換句話說,字串中的第一個字元位於索引 0,而不是索引 1。 若要擷取從指定字元位置開始的子字串,並繼續到字串結尾,請呼叫 Substring(Int32) 方法。

備註

這個方法不會修改目前實例的值。 相反地,它會傳回新的字串,其中包含 lengthstartIndex 目前字串中的位置開始的字元。

參數 length 代表要從目前字串實例擷取的字元總數。 這包括在索引 startIndex 中找到的起始字元。 換句話說,方法 Substring 會嘗試從索引擷取字元到索引 + startIndexstartIndexlength - 1。

若要擷取以特定字元或字元序列開頭的子字串,請呼叫 或 之類的 IndexOfLastIndexOf 方法來取得 的值 startIndex

如果子字串應該從 startIndex 延伸至指定的字元序列,您可以呼叫 方法,例如 IndexOfLastIndexOf ,以取得結束符或字元序列的索引。 然後,您可以將該值轉換成字串中的索引位置,如下所示:

  • 如果您已搜尋要標示子字串結尾的單一字元, length 參數等於 - startIndexendIndex + 1,其中 endIndex 是 或 LastIndexOf 方法的 IndexOf 傳回值。 下列範例會從字串擷取 「b」 字元的連續區塊。

    C#
    String s = "aaaaabbbcccccccdd";
    Char charRange = 'b';
    int startIndex = s.IndexOf(charRange);
    int endIndex = s.LastIndexOf(charRange);
    int length = endIndex - startIndex + 1;
    Console.WriteLine("{0}.Substring({1}, {2}) = {3}",
                    s, startIndex, length, 
                    s.Substring(startIndex, length));
    
    // The example displays the following output:
    //       aaaaabbbcccccccdd.Substring(5, 3) = bbb
    
  • 如果您已搜尋要標記子字串結尾的多個字元, length 參數會 - + startIndexendIndexendMatchLength 等於 ,其中 endIndex 是 或 LastIndexOf 方法的 IndexOf 傳回值,而 endMatchLength 是標記子字串結尾的字元序列長度。 下列範例會擷取包含 XML <definition> 元素的文字區塊。

    C#
    String s = "<term>extant<definition>still in existence</definition></term>";
    String searchString = "<definition>";
    int startIndex = s.IndexOf(searchString);
    searchString = "</" + searchString.Substring(1);
    int endIndex = s.IndexOf(searchString);
    String substring = s.Substring(startIndex, endIndex + searchString.Length - startIndex);
    Console.WriteLine("Original string: {0}", s);
    Console.WriteLine("Substring;       {0}", substring); 
    
    // The example displays the following output:
    //     Original string: <term>extant<definition>still in existence</definition></term>
    //     Substring;       <definition>still in existence</definition>
    
  • 如果子字串結尾未包含字元或字元序列, length 參數會 endIndex - startIndex 等於 ,其中 endIndex 是 或 LastIndexOf 方法的 IndexOf 傳回值。

如果 startIndex 等於零且 length 等於目前字串的長度,則方法會傳回原始字串未變更。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0