C# 字符串

2021-07-08 16:06

阅读:473

标签:csharp   visual   isnull   lse   ble   idg   视觉   spl   数组初始化   

from: https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/strings/

C#提供了别名string来代表System.String类。如果在代码中使用String类,必须载入包 using System;而使用内建的别名string则不需要。

文本在内部存储为 Char 对象的依序只读集合。 在 C# 字符串末尾没有 null 终止字符;因此,一个 C# 字符串可以包含任何数量的嵌入的 null 字符 (‘\0‘)。 字符串的 Length 属性表示其包含的 Char 对象数量,而非 Unicode 字符数。 若要访问字符串中的各个 Unicode 码位,请使用 StringInfo 对象。

声明和初始化字符串

请注意,不要使用 new 运算符创建字符串对象,除非使用字符数组初始化字符串。

使用 Empty 常量值初始化字符串,以新建字符串长度为零的 String 对象。 长度为零的字符串文本表示法是“”。 通过使用 Empty 值(而不是 null)初始化字符串,可以减少 NullReferenceException 发生的可能性。 尝试访问字符串前,先使用静态 IsNullOrEmpty(String) 方法验证字符串的值。

字符串对象的不可变性

字符串对象是“不可变的”:它们在创建后无法更改。 看起来是在修改字符串的所有 String 方法和 C# 运算符实际上都是在新的字符串对象中返回结果。

转义字符和逐字字符串

常规字符串自动转义字符。关于字符串转义序列,请查询官网:转义序列。

当字符串文本包含反斜杠字符(例如在文件路径中)时,出于便捷性和更强的可读性的考虑,使用逐字字符串。 由于逐字字符串将新的行字符作为字符串文本的一部分保留,因此可将其用于初始化多行字符串。 使用双引号在逐字字符串内部嵌入引号。

string filePath = @"C:\Users\scoleridge\Documents\";
//Output: C:\Users\scoleridge\Documents\

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

格式化字符串

使用静态 Format 方法,并在大括号中嵌入将在运行时被其他值替换的占位符,从而创建格式字符串。

WriteLine 方法的一个重载将格式字符串用作参数。 因此,可以仅嵌入格式字符串文本,而无需显式调用该方法。 不过,如果使用 WriteLine 方法在 Visual Studio“输出”窗口中显示调试输出,必须显式调用 Format 方法,因为 WriteLine 仅接受字符串,而不接受格式字符串。 

子字符串

string s3 = "Visual C# Express";
System.Console.WriteLine(s3.Substring(7, 2));
// Output: "C#"

System.Console.WriteLine(s3.Replace("C#", "Basic"));
// Output: "Visual Basic Express"

// Index values are zero-based
int index = s3.IndexOf("C");
// index = 7

访问单个字符

由于System.String对象只能够访问字符,而无法修改,可以换用 StringBuilder 对象重新实现字符串。

string question = "hOW DOES mICROSOFT wORD DEAL WITH THE cAPS lOCK KEY?";
System.Text.StringBuilder sb = new System.Text.StringBuilder(question);

Null 字符串和空字符串

空字符串是包含零个字符的 System.String 对象实例。 赋值如下: string s = String.Empty;  

相比较而言,null 字符串并不指 System.String 对象实例,只要尝试对 null 字符串调用方法,都会引发 NullReferenceException。 但是,可以在串联和与其他字符串的比较操作中使用 null 字符串。 

string str = "hello";
string nullStr = null;
string emptyStr = String.Empty;

string tempStr = str + nullStr;
// Output of the following line: hello
Console.WriteLine(tempStr);

bool b = (emptyStr == nullStr);
// Output of the following line: False
Console.WriteLine(b);

// The following line creates a new empty string.
string newStr = emptyStr + nullStr;

// Null strings and empty strings behave differently. The following
// two lines display 0.
Console.WriteLine(emptyStr.Length);
Console.WriteLine(newStr.Length);
// The following line raises a NullReferenceException.
Console.WriteLine(nullStr.Length);

// The null character can be displayed and counted, like other chars.
string s1 = "\x0" + "abc";
string s2 = "abc" + "\x0";
// Output of the following line: * abc*
Console.WriteLine("*" + s1 + "*");
// Output of the following line: *abc *
Console.WriteLine("*" + s2 + "*");
// Output of the following line: 4
Console.WriteLine(s2.Length);

使用 StringBuilder

.NET 中的字符串操作进行了高度的优化,在大多数情况下不会显著影响性能。 但是,在某些情况下(例如,执行数百次或数千次的紧密循环),字符串操作可能影响性能。 StringBuilder 类创建字符串缓冲区,用于在程序执行多个字符串操控时提升性能。 使用 StringBuilder 字符串,还可以重新分配各个字符,而内置字符串数据类型则不支持这样做。

System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Create a string composed of numbers 0 - 9
for (int i = 0; i 10; i++)
{
    sb.Append(i.ToString());
}
System.Console.WriteLine(sb);  // displays 0123456789

// Copy one character of the string (not possible with a System.String)
sb[0] = sb[9];

System.Console.WriteLine(sb);  // displays 9123456789

字符串、扩展方法和 LINQ

由于 String 类型实现 IEnumerable,因此可以对字符串使用 Enumerable 类中定义的扩展方法。 为了避免视觉干扰,这些方法已从 String 类型的 IntelliSense 中排除,但它们仍然可用。 此外,还可以使用字符串上的 LINQ 查询表达式。 有关详细信息,请参阅 LINQ 和字符串。

 

C# 字符串

标签:csharp   visual   isnull   lse   ble   idg   视觉   spl   数组初始化   

原文地址:https://www.cnblogs.com/brt3/p/9736352.html

上一篇:Windy数

下一篇:windows下搭建vue开发环境


评论


亲,登录后才可以留言!