修剪和移除 .NET 中字串的字元

如果您將句子剖析成個別文字,最後可能會得到許多文字,但文字任一端有空格 (也稱為空白字元)。 在這種情況下,您可以使用 System.String 類別中的其中一個修剪方法,從字串中的指定位置移除任意數目的空格或其他字元。 下表描述可用的修剪方法:

方法名稱 使用
String.Trim 將字元陣列中字串開頭和結尾指定的空格或空白字元移除。
String.TrimEnd 從字串尾端移除字元陣列中指定的字元。
String.TrimStart 從字串開頭移除字元陣列中指定的字元。
String.Remove 從字串中指定的索引位置移除指定的字元數。

Trim

您可以使用 String.Trim 方法,輕鬆地移除字串頭尾的空格,如下列範例所示:

String^ MyString = " Big   ";
Console::WriteLine("Hello{0}World!", MyString);
String^ TrimString = MyString->Trim();
Console::WriteLine("Hello{0}World!", TrimString);
// The example displays the following output:
//       Hello Big   World!
//       HelloBigWorld!        
string MyString = " Big   ";
Console.WriteLine("Hello{0}World!", MyString);
string TrimString = MyString.Trim();
Console.WriteLine("Hello{0}World!", TrimString);
//       The example displays the following output:
//             Hello Big   World!
//             HelloBigWorld!
Dim MyString As String = " Big   "
Console.WriteLine("Hello{0}World!", MyString)
Dim TrimString As String = MyString.Trim()
Console.WriteLine("Hello{0}World!", TrimString)
' The example displays the following output:
'       Hello Big   World!
'       HelloBigWorld!        

您也可以將字元陣列中字串開頭和結尾指定的字元移除。 下列範例將會移除空白字元、句號和星號:

using System;

public class Example
{
   public static void Main()
   {
      String header = "* A Short String. *";
      Console.WriteLine(header);
      Console.WriteLine(header.Trim( new Char[] { ' ', '*', '.' } ));
   }
}
// The example displays the following output:
//       * A Short String. *
//       A Short String
Module Example
    Public Sub Main()
        Dim header As String = "* A Short String. *"
        Console.WriteLine(header)
        Console.WriteLine(header.Trim({" "c, "*"c, "."c}))
    End Sub
End Module
' The example displays the following output:
'       * A Short String. *
'       A Short String

TrimEnd

String.TrimEnd 方法會從字串結尾移除字元,並建立新的字串物件。 系統會將字元陣列傳遞給這個方法,以指定要移除的字元。 字元陣列中的項目順序不會影響修剪作業。 一旦找到陣列中未指定的字元時,修剪作業就會停止。

下列範例會使用 TrimEnd 方法,移除字串的最後一個字母。 在此範例中,'r' 字元和 'W' 字元的位置顛倒,用以說明字元陣列中的順序並不重要。 請注意,此程式碼會移除 MyString 的最後一個字以及第一個字的一部分。

String^ MyString = "Hello World!";
array<Char>^ MyChar = {'r','o','W','l','d','!',' '};
String^ NewString = MyString->TrimEnd(MyChar);
Console::WriteLine(NewString);
string MyString = "Hello World!";
char[] MyChar = {'r','o','W','l','d','!',' '};
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);
Dim MyString As String = "Hello World!"
Dim MyChar() As Char = {"r", "o", "W", "l", "d", "!", " "}
Dim NewString As String = MyString.TrimEnd(MyChar)
Console.WriteLine(NewString)

此程式碼會讓主控台顯示 He

下列範例會使用 TrimEnd 方法,移除字串的最後一個字。 在這個程式碼中,Hello 文字後接一個逗號,不過由於要修剪的字元陣列中未指定逗號,因此修剪作業會在逗號處停止。

String^ MyString = "Hello, World!";
array<Char>^ MyChar = {'r','o','W','l','d','!',' '};
String^ NewString = MyString->TrimEnd(MyChar);
Console::WriteLine(NewString);
string MyString = "Hello, World!";
char[] MyChar = {'r','o','W','l','d','!',' '};
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);
Dim MyString As String = "Hello, World!"
Dim MyChar() As Char = {"r", "o", "W", "l", "d", "!", " "}
Dim NewString As String = MyString.TrimEnd(MyChar)
Console.WriteLine(NewString)

此程式碼會讓主控台顯示 Hello,

TrimStart

String.TrimStart 方法與 String.TrimEnd 方法類似,只是它會移除現有字串物件開頭的字元以建立新字串。 系統會將字元陣列傳遞給 TrimStart 方法,以指定要移除的字元。 使用 TrimEnd 時,字元陣列中的項目順序不會影響修剪作業。 一旦找到陣列中未指定的字元時,修剪作業就會停止。

下列範例會移除字串的第一個文字。 在此範例中,'l' 字元和 'H' 字元的位置顛倒,用以說明字元陣列中的順序並不重要。

String^ MyString = "Hello World!";
array<Char>^ MyChar = {'e', 'H','l','o',' ' };
String^ NewString = MyString->TrimStart(MyChar);
Console::WriteLine(NewString);
string MyString = "Hello World!";
char[] MyChar = {'e', 'H','l','o',' ' };
string NewString = MyString.TrimStart(MyChar);
Console.WriteLine(NewString);
Dim MyString As String = "Hello World!"
Dim MyChar() As Char = {"e", "H", "l", "o", " "}
Dim NewString As String = MyString.TrimStart(MyChar)
Console.WriteLine(NewString)

此程式碼會讓主控台顯示 World!

移除

String.Remove 方法會從現有字串中的指定位置開始,移除指定的字元數。 這個方法會假設以零為起始的索引。

下列範例會於字串中以零為起始之索引的位置五開始,從字串中移除 10 個字元。

String^ MyString = "Hello Beautiful World!";
Console::WriteLine(MyString->Remove(5,10));
// The example displays the following output:
//         Hello World!        
string MyString = "Hello Beautiful World!";
Console.WriteLine(MyString.Remove(5,10));
// The example displays the following output:
//         Hello World!
Dim MyString As String = "Hello Beautiful World!"
Console.WriteLine(MyString.Remove(5, 10))
' The example displays the following output:
'         Hello World!        

Replace

您也可以從字串中移除指定的字元或子字串,方法是呼叫 String.Replace(String, String) 方法並指定空字串 (String.Empty) 做為取代。 下列範例將會從字串中移除所有逗號:

using System;

public class Example
{
   public static void Main()
   {
      String phrase = "a cold, dark night";
      Console.WriteLine("Before: {0}", phrase);
      phrase = phrase.Replace(",", "");
      Console.WriteLine("After: {0}", phrase);
   }
}
// The example displays the following output:
//       Before: a cold, dark night
//       After: a cold dark night
Module Example
    Public Sub Main()
        Dim phrase As String = "a cold, dark night"
        Console.WriteLine("Before: {0}", phrase)
        phrase = phrase.Replace(",", "")
        Console.WriteLine("After: {0}", phrase)
    End Sub
End Module
' The example displays the following output:
'       Before: a cold, dark night
'       After: a cold dark night

另請參閱