共用方式為


建立新字串

更新:2007 年 11 月

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

使用指派建立字串

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

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

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

傳回字串的方法

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

方法名稱

做法

String.Format

從一組輸入物件建置 (Build) 格式化的字串

String.Concat

從兩個或多個字串建置字串

String.Join

藉由組合字串陣列來建置新字串

String.Insert

藉由將字串插入現有字串的指定索引中來建立新字串

String.CopyTo

將字串中的指定字元複製到字元陣列中的指定位置

Format

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

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

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.
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.

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

Concat

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

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 helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
//      Hello World!

Join

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

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

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[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
Console.WriteLine(String.Join(" ", words));
// The example displays the following output:
//      Hello and welcome to my world!

Insert

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

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

CopyTo

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

下列範例會使用 CopyTo 方法,將 "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
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

請參閱

概念

複合格式

其他資源

基本字串作業