Share via


新建 .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!

Insert

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

请参阅