在 .NET 中建立新字串

.NET 允許使用簡單的指派來建立字串,並且還可以多載類別建構函式,以支援使用多個不同參數來建立字串。 .NET 也會在 System.String 類別中提供數個方法,藉由組合多個字串、字串陣列或物件來建立新的字串物件。

使用指派建立字串

建立新 String 物件的最簡單方式,就是將字串常值指派給 String 物件。

使用類別建構函式建立字串

您可以使用 String 類別建構函式的多載,從字元陣列建立字串。 您也可以讓某個特定字元重複指定的次數,藉此建立新字串。

傳回字串的方法

下表列出數個可傳回新字串物件的有用方法。

方法名稱 使用
String.Format 從一組輸入物件建立格式化的字串。
String.Concat 從兩個或多個字串建立字串。
String.Join 藉由組合字串陣列來建立新字串。
String.Insert 藉由將字串插入現有字串的指定索引中來建立新字串。
String.CopyTo 將字串中的指定字元複製到字元陣列中的指定位置。

格式

您可以使用 String.Format 方法,來建立格式化的字串和串連代表多個物件的字串。 這個方法會自動將任何傳遞的物件轉換為字串。 例如,如果您的應用程式必須向使用者顯示 Int32 值和 DateTime 值,您可以使用 Format 方法,輕鬆地建構代表這些值的字串。 如需搭配此方法使用之格式慣例的資訊,請參閱複合格式一節。

下列範例會使用 Format 方法來建立使用整數變數的字串。

int numberOfFleas = 12;
string miscInfo = String.Format("Your dog has {0} fleas. " +
                                "It is time to get a flea collar. " +
                                "The current universal date is: {1:u}.",
                                numberOfFleas, DateTime.Now);
Console.WriteLine(miscInfo);
// The example displays the following output:
//       Your dog has 12 fleas. It is time to get a flea collar.
//       The current universal date is: 2008-03-28 13:31:40Z.
Dim numberOfFleas As Integer = 12
Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                       "It is time to get a flea collar. " & _
                                       "The current universal date is: {1:u}.", _
                                       numberOfFleas, Date.Now)
Console.WriteLine(miscInfo)
' The example displays the following output:
'       Your dog has 12 fleas. It is time to get a flea collar. 
'       The current universal date is: 2008-03-28 13:31:40Z.

在此範例中,DateTime.Now 會以和目前執行緒相關聯之文化特性所指定的方式來顯示目前的日期和時間。

Concat

使用 String.Concat 方法,可輕鬆地從兩個或多個現有物件建立新的字串物件。 它提供與語言無關的方式來串連字串。 這個方法接受任何衍生自 System.Object 的類別。 下列範例會從現有的兩個字串物件和一個分隔字元建立字串。

string helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
//      Hello World!
Dim helloString1 As String = "Hello"
Dim helloString2 As String = "World!"
Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
' The example displays the following output:
'      Hello World!

聯結

String.Join 方法會從字串陣列和分隔符號字串建立新的字串。 如果您想要將多個字串串連在一起,建立以逗號分隔的清單,這個方法會很有用。

下列範例會使用空格來繫結字串陣列。

string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
Console.WriteLine(String.Join(" ", words));
// The example displays the following output:
//      Hello and welcome to my world!
Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
Console.WriteLine(String.Join(" ", words))
' The example displays the following output:
'      Hello and welcome to my world!

插入​​

String.Insert 方法會將字串插入另一個字串中的指定位置,藉以建立新的字串。 這個方法會使用以零起始的索引。 下列範例會將字串插入 MyString 的第五個索引位置,並使用這個值來建立新的字串。

string sentence = "Once a time.";
 Console.WriteLine(sentence.Insert(4, " upon"));
 // The example displays the following output:
 //      Once upon a time.
Dim sentence As String = "Once a time."
Console.WriteLine(sentence.Insert(4, " upon"))
' The example displays the following output:
'      Once upon a time.

CopyTo

String.CopyTo 方法會將部分字串複製到字元陣列中。 您可以同時指定字串的開頭索引和要複製的字元數。 這個方法會使用來源索引、字元陣列、目的索引和要複製的字元數。 所有索引都是以零起始。

下列範例會使用 CopyTo 方法,將 "Hello" 這個字的所有字元從字串物件複製到字元陣列的第一個索引位置。

string greeting = "Hello World!";
char[] charArray = {'W','h','e','r','e'};
Console.WriteLine("The original character array: {0}", new string(charArray));
greeting.CopyTo(0, charArray,0 ,5);
Console.WriteLine("The new character array: {0}", new string(charArray));
// The example displays the following output:
//       The original character array: Where
//       The new character array: Hello
Dim greeting As String = "Hello World!"
Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
Console.WriteLine("The original character array: {0}", New String(charArray))
greeting.CopyTo(0, charArray, 0, 5)
Console.WriteLine("The new character array: {0}", New String(charArray))
' The example displays the following output:
'       The original character array: Where
'       The new character array: Hello

另請參閱