共用方式為


從字串擷取子字串

本文涵蓋一些適用於擷取部份字串的不同技巧。

String.Split 方法

String.Split 提供的少數多載可以協助您根據指定的一或多個分隔字元,將一個字串分成一組子字串。 您可以選擇限制最終結果中的子字串總數、修剪子字串中的空白字元,或排除空的子字串。

下列範例顯示三個不同的 String.Split() 多載。 第一個範例呼叫 Split(Char[]) 多載,而不傳遞任何分隔字元。 如果您未指定任何分隔字元,String.Split() 會使用預設分隔符號分割字串,預設分隔符號為空白字元。

string s = "You win some. You lose some.";

string[] subs = s.Split();

foreach (string sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some.
// Substring: You
// Substring: lose
// Substring: some.
Dim s As String = "You win some. You lose some."
Dim subs As String() = s.Split()

For Each substring As String In subs
    Console.WriteLine("Substring: {0}", substring)
Next

' This example produces the following output:
'
' Substring: You
' Substring: win
' Substring: some.
' Substring: You
' Substring: lose
' Substring: some.

如您所見,句號字元 (.) 包含在兩個子字串中。 如果您想要排除句號字元,您可以將句號字元新增為額外的分隔符號。 下一個範例示範如何執行這項作業。

string s = "You win some. You lose some.";

string[] subs = s.Split(' ', '.');

foreach (string sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some
// Substring:
// Substring: You
// Substring: lose
// Substring: some
// Substring:
Dim s As String = "You win some. You lose some."
Dim subs As String() = s.Split(" "c, "."c)

For Each substring As String In subs
    Console.WriteLine("Substring: {0}", substring)
Next

' This example produces the following output:
'
' Substring: You
' Substring: win
' Substring: some
' Substring:
' Substring: You
' Substring: lose
' Substring: some
' Substring:

句點從子字串消失,現在卻包含兩個額外的空白子字串。 這些空白子字串代表文字與後面句點之間的子字串。 若要從產生的陣列中省略空白子字串,您可以呼叫 Split(Char[], StringSplitOptions) 多載並指定 StringSplitOptions.RemoveEmptyEntries 做為 options 參數。

string s = "You win some. You lose some.";
char[] separators = new char[] { ' ', '.' };

string[] subs = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);

foreach (string sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some
// Substring: You
// Substring: lose
// Substring: some
Dim s As String = "You win some. You lose some."
Dim separators As Char() = New Char() {" "c, "."c}
Dim subs As String() = s.Split(separators, StringSplitOptions.RemoveEmptyEntries)

For Each substring As String In subs
    Console.WriteLine("Substring: {0}", substring)
Next

' This example produces the following output:
'
' Substring: You
' Substring: win
' Substring: some
' Substring: You
' Substring: lose
' Substring: some

規則運算式

如果您的字串符合固定模式,您可以使用規則運算式擷取和處理其元素。 例如,如果字串採用「數字 運算元 數字」形式,則您可以使用規則運算式擷取和處理字串的元素。 以下是範例:

String[] expressions = { "16 + 21", "31 * 3", "28 / 3",
                       "42 - 18", "12 * 7",
                       "2, 4, 6, 8" };
String pattern = @"(\d+)\s+([-+*/])\s+(\d+)";

foreach (string expression in expressions)
{
    foreach (System.Text.RegularExpressions.Match m in
    System.Text.RegularExpressions.Regex.Matches(expression, pattern))
    {
        int value1 = Int32.Parse(m.Groups[1].Value);
        int value2 = Int32.Parse(m.Groups[3].Value);
        switch (m.Groups[2].Value)
        {
            case "+":
                Console.WriteLine("{0} = {1}", m.Value, value1 + value2);
                break;
            case "-":
                Console.WriteLine("{0} = {1}", m.Value, value1 - value2);
                break;
            case "*":
                Console.WriteLine("{0} = {1}", m.Value, value1 * value2);
                break;
            case "/":
                Console.WriteLine("{0} = {1:N2}", m.Value, value1 / value2);
                break;
        }
    }
}

// The example displays the following output:
//       16 + 21 = 37
//       31 * 3 = 93
//       28 / 3 = 9.33
//       42 - 18 = 24
//       12 * 7 = 84
Dim expressions() As String = {"16 + 21", "31 * 3", "28 / 3",
                              "42 - 18", "12 * 7",
                              "2, 4, 6, 8"}

Dim pattern As String = "(\d+)\s+([-+*/])\s+(\d+)"
For Each expression In expressions
    For Each m As Match In Regex.Matches(expression, pattern)
        Dim value1 As Integer = Int32.Parse(m.Groups(1).Value)
        Dim value2 As Integer = Int32.Parse(m.Groups(3).Value)
        Select Case m.Groups(2).Value
            Case "+"
                Console.WriteLine("{0} = {1}", m.Value, value1 + value2)
            Case "-"
                Console.WriteLine("{0} = {1}", m.Value, value1 - value2)
            Case "*"
                Console.WriteLine("{0} = {1}", m.Value, value1 * value2)
            Case "/"
                Console.WriteLine("{0} = {1:N2}", m.Value, value1 / value2)
        End Select
    Next
Next

' The example displays the following output:
'       16 + 21 = 37
'       31 * 3 = 93
'       28 / 3 = 9.33
'       42 - 18 = 24
'       12 * 7 = 84

規則運算式模式 (\d+)\s+([-+*/])\s+(\d+) 的定義如下:

模式 描述
(\d+) 比對一個或多個十進位數字。 這是第一個擷取群組。
\s+ 比對一個或多個空白字元。
([-+*/]) 比對算術運算子符號 (+、-、* 或 /)。 這是第二個擷取群組。
\s+ 比對一個或多個空白字元。
(\d+) 比對一個或多個十進位數字。 這是第三個擷取群組。

您也可以使用規則運算式,根據模式擷取字串中的子字串,而非依據固定的字元組合。 以下發生的任一情況為常見案例:

  • String 執行個體中,一或多個分隔符號字元「不一定」是做為分隔符號之用。

  • 分隔符號字元的序列和數量為變數或未知數。

例如,Split 方法無法用來分割下列字串,因為 \n (新行字元) 字元的數量是變數,而且不一定是做為分隔符號之用。

[This is captured\ntext.]\n\n[\n[This is more captured text.]\n]
\n[Some more captured text:\n   Option1\n   Option2][Terse text.]

規則運算式可以輕鬆地分割此字串,如下列範例所示。

String input = "[This is captured\ntext.]\n\n[\n" +
               "[This is more captured text.]\n]\n" +
               "[Some more captured text:\n   Option1" +
               "\n   Option2][Terse text.]";
String pattern = @"\[([^\[\]]+)\]";
int ctr = 0;

foreach (System.Text.RegularExpressions.Match m in
   System.Text.RegularExpressions.Regex.Matches(input, pattern))
{
    Console.WriteLine("{0}: {1}", ++ctr, m.Groups[1].Value);
}

// The example displays the following output:
//       1: This is captured
//       text.
//       2: This is more captured text.
//       3: Some more captured text:
//          Option1
//          Option2
//       4: Terse text.
Dim input As String = String.Format("[This is captured{0}text.]" +
                                  "{0}{0}[{0}[This is more " +
                                  "captured text.]{0}{0}" +
                                  "[Some more captured text:" +
                                  "{0}   Option1" +
                                  "{0}   Option2][Terse text.]",
                                  vbCrLf)
Dim pattern As String = "\[([^\[\]]+)\]"
Dim ctr As Integer = 0
For Each m As Match In Regex.Matches(input, pattern)
    ctr += 1
    Console.WriteLine("{0}: {1}", ctr, m.Groups(1).Value)
Next

' The example displays the following output:
'       1: This is captured
'       text.
'       2: This is more captured text.
'       3: Some more captured text:
'          Option1
'          Option2
'       4: Terse text.

規則運算式模式 \[([^\[\]]+)\] 的定義如下:

模式 描述
\[ 比對左中括弧。
([^\[\]]+) 一或多次比對非左中括弧或右中括弧的任何字元。 這是第一個擷取群組。
\] 比對右中括弧。

Regex.Split 方法與 String.Split 幾乎完全相同,不同之處在於前者根據規則運算式模式分割字串,而不是根據固定的字元組合。 舉例來說,下列範例使用 Regex.Split 方法分割字串,字串中包含的子字串以連字號與其他字元的各種組合分隔。

String input = "abacus -- alabaster - * - atrium -+- " +
               "any -*- actual - + - armoire - - alarm";
String pattern = @"\s-\s?[+*]?\s?-\s";
String[] elements = System.Text.RegularExpressions.Regex.Split(input, pattern);

foreach (string element in elements)
    Console.WriteLine(element);

// The example displays the following output:
//       abacus
//       alabaster
//       atrium
//       any
//       actual
//       armoire
//       alarm
Dim input As String = "abacus -- alabaster - * - atrium -+- " +
                    "any -*- actual - + - armoire - - alarm"
Dim pattern As String = "\s-\s?[+*]?\s?-\s"
Dim elements() As String = Regex.Split(input, pattern)
For Each element In elements
    Console.WriteLine(element)
Next

' The example displays the following output:
'       abacus
'       alabaster
'       atrium
'       any
'       actual
'       armoire
'       alarm

規則運算式模式 \s-\s?[+*]?\s?-\s 的定義如下:

模式 描述
\s- 比對後面接著連字號的空白字元。
\s? 比對零個或一個空白字元。
[+*]? 比對出現零次或一次的 + 或 * 字元。
\s? 比對零個或一個空白字元。
-\s 比對後面接著空白字元的連字號。

String.IndexOf 和 String.Substring 方法

如果您對字串中的所有子字串不感興趣,您可能會想要使用下列其中一個字串比較方法,傳回比對開始位置的索引。 然後,您可以呼叫 Substring 方法擷取您想要的子字串。 字串比較方法包括:

  • IndexOf,此方法傳回字串執行個體中第一次出現字元或字串的索引位置,索引從零開始。

  • IndexOfAny,此方法傳回目前字串執行個體中第一次出現字元陣列中任何字元的索引位置,索引從零開始。

  • LastIndexOf,此方法傳回字串執行個體中最後一次出現字元或字串的索引位置,索引從零開始。

  • LastIndexOfAny,此方法傳回目前字串執行個體中最後一次出現字元陣列中任何字元的索引位置,索引從零開始。

下列範例使用 IndexOf 方法尋找字串中的句點。 然後,使用 Substring 方法傳回完整的句子。

String s = "This is the first sentence in a string. " +
               "More sentences will follow. For example, " +
               "this is the third sentence. This is the " +
               "fourth. And this is the fifth and final " +
               "sentence.";
var sentences = new List<String>();
int start = 0;
int position;

// Extract sentences from the string.
do
{
    position = s.IndexOf('.', start);
    if (position >= 0)
    {
        sentences.Add(s.Substring(start, position - start + 1).Trim());
        start = position + 1;
    }
} while (position > 0);

// Display the sentences.
foreach (var sentence in sentences)
    Console.WriteLine(sentence);

// The example displays the following output:
//       This is the first sentence in a string.
//       More sentences will follow.
//       For example, this is the third sentence.
//       This is the fourth.
//       And this is the fifth and final sentence.
    Dim input As String = "This is the first sentence in a string. " +
                        "More sentences will follow. For example, " +
                        "this is the third sentence. This is the " +
                        "fourth. And this is the fifth and final " +
                        "sentence."
    Dim sentences As New List(Of String)
    Dim start As Integer = 0
    Dim position As Integer

    ' Extract sentences from the string.
    Do
        position = input.IndexOf("."c, start)
        If position >= 0 Then
            sentences.Add(input.Substring(start, position - start + 1).Trim())
            start = position + 1
        End If
    Loop While position > 0

    ' Display the sentences.
    For Each sentence In sentences
        Console.WriteLine(sentence)
    Next
End Sub

' The example displays the following output:
'       This is the first sentence in a string.
'       More sentences will follow.
'       For example, this is the third sentence.
'       This is the fourth.
'       And this is the fifth and final sentence.

另請參閱