有关 Microsoft 开发人员工具和技术的深度文章
此浏览器不再受支持。
请升级到 Microsoft Edge 以使用最新的功能、安全更新和技术支持。
Learn有关 Microsoft 开发人员工具和技术的深度文章 经 Microsoft 审核的技术问题和解答 Microsoft 开发人员工具和技术的代码示例库 来自 Microsoft 专家的数千小时原创节目 适用于组织的 Microsoft Learn 访问特选资源,以提升团队技能水平,缩小技能差距。
适用于组织的 Microsoft Learn 访问特选资源,以提升团队技能水平,缩小技能差距。
适用于组织的 Microsoft Learn 访问特选资源,以提升团队技能水平,缩小技能差距。
适用于组织的 Microsoft Learn 访问特选资源,以提升团队技能水平,缩小技能差距。
字符串是 String 类型的对象,它的值是文本。 在内部,文本被存储为 Char 对象的顺序只读集合。 C# 字符串末尾没有以 null 结尾的字符;因此 C# 字符串可以包含任意数目的嵌入式 null 字符(“ ”)。 字符串的 Length 属性代表它包含的 Char 对象的数量,而不是 Unicode 字符的数量。 若要访问字符串中的各个 Unicode 码位,请使用 StringInfo 对象。
在 C# 中,string 关键字是 String 的别名。 因此,String 与 string 等效,您可以根据自己的喜好选择命名约定。 String 类提供了很多用于安全地创建、操作和比较字符串的方法。 此外,C# 语言还重载某些运算符来简化常见的字符串操作。 有关关键字的更多信息,请参见string(C# 参考)。 有关类型及其方法的更多信息,请参见 String。
可以通过各种方式来声明和初始化字符串,如下面的示例所示:
// Declare without initializing. string message1; // Initialize to null. string message2 = null; // Initialize as an empty string. // Use the Empty constant instead of the literal "". string message3 = System.String.Empty; //Initialize with a regular string literal. string oldPath = "c:\Program Files\Microsoft Visual Studio 8.0"; // Initialize with a verbatim string literal. string newPath = @"c:Program FilesMicrosoft Visual Studio 9.0"; // Use System.String if you prefer. System.String greeting = "Hello World!"; // In local variables (i.e. within a method body) // you can use implicit typing. var temp = "I'm still a strongly-typed System.String!"; // Use a const string to prevent 'message4' from // being used to store another string value. const string message4 = "You can't get rid of me!"; // Use the String constructor only when creating // a string from a char*, char[], or sbyte*. See // System.String documentation for details. char[] letters = { 'A', 'B', 'C' }; string alphabet = new string(letters);
注意,除了在使用字符数组初始化字符串时以外,不要使用 new 运算符创建字符串对象。
使用 Empty 常量值初始化字符串可新建字符串长度为零的 String 对象。 零长度字符串的字符串表示形式为 ""。 使用 Empty 值(而不是 null)初始化字符串可以降低发生 NullReferenceException 的可能性。 请在尝试访问字符串之前使用静态 IsNullOrEmpty(String) 方法验证字符串的值。
字符串对象是不可变的:即它们创建之后就无法更改。 所有看似修改字符串的 String 方法和 C# 运算符实际上都以新字符串对象的形式返回结果。 在下面的示例中,当连接 s1 和 s2 的内容以形成一个字符串时,不会修改两个原始字符串。 += 运算符会创建一个包含组合内容的新字符串。 这个新对象赋给变量 s1,而最初赋给 s1 的对象由于没有其他任何变量包含对它的引用而释放,用于垃圾回收。
string s1 = "A string is more "; string s2 = "than the sum of its chars."; // Concatenate s1 and s2. This actually creates a new // string object and stores it in s1, releasing the // reference to the original object. s1 += s2; System.Console.WriteLine(s1); // Output: A string is more than the sum of its chars.
由于“修改”字符串实际上是创建新字符串,因此创建对字符串的引用时必须谨慎。 如果创建了对字符串的引用,然后“修改”原始字符串,则该引用指向的仍是原始对象,而不是修改字符串时创建的新对象。 下面的代码说明了这种行为:
string s1 = "Hello "; string s2 = s1; s1 += "World"; System.Console.WriteLine(s2); //Output: Hello
有关如何创建基于修改(例如搜索和替换原始字符串的操作)的新字符串的更多信息,请参见如何:修改字符串内容(C# 编程指南)。
如果必须嵌入 C# 提供的转义符,则应使用正则字符串,如下面的示例所示:
string columns = "Column 1tColumn 2tColumn 3"; //Output: Column 1 Column 2 Column 3 string rows = "Row 1rnRow 2rnRow 3"; /* Output: Row 1 Row 2 Row 3 */ string title = ""The u00C6olean Harp", by Samuel Taylor Coleridge"; //Output: "The Æolean Harp", by Samuel Taylor Coleridge
如果字符串文本包含反斜杠字符(例如在文件路径中),为方便起见和提高可读性,应使用原义字符串。 由于原义字符串保留换行符作为字符串文本的一部分,因此可用于初始化多行字符串。 在原义字符串中嵌入引号时请使用双引号。 下面的示例演示原义字符串的一些常见用途:
string filePath = @"C:UsersscoleridgeDocuments"; //Output: C:UsersscoleridgeDocuments string text = @"My pensive SARA ! thy soft cheek reclined Thus on mine arm, most soothing sweet it is To sit beside our Cot,..."; /* Output: My pensive SARA ! thy soft cheek reclined Thus on mine arm, most soothing sweet it is To sit beside our Cot,... */ string quote = @"Her name was ""Sara."""; //Output: Her name was "Sara."
转义序列
字符名称
Unicode 编码
'
单引号
0x0027
"
双引号
0x0022
\
反斜杠
0x005C