导入
字符串入门
学习如何在 C# 中处理不可变字符串时创建、检查和管理内存。
代码示例
1string someText = "Hello World!";
1// 使用字符串字面量 2string someText = "Hello World!"; 3Console.WriteLine(someText); 4 5// 通过类的构造函数之一 6string someOtherText = new string(['h', 'u', 'n', 't']); 7Console.WriteLine(someOtherText);
代码示例
1string? someString = null;
1string? nullString = null; 2string emptyString = ""; 3string spaceString = " "; 4 5// 返回 true,因为字符串为 null 6Console.WriteLine(string.IsNullOrEmpty(nullString)); 7 8// 返回 true,因为字符串只包含空格 9Console.WriteLine(string.IsNullOrWhiteSpace(spaceString));
1string folderPath = @"C:\Users\Admin\Documents"; 2 3string jsonPayload = """ 4{ 5 "user": "Alice", 6 "status": "Active" 7} 8"""; 9 10Console.WriteLine(folderPath); 11Console.WriteLine(jsonPayload);
1string text = "Hello World!"; 2Console.WriteLine(text[0]); // 输出将为 H 3Console.WriteLine(text.Length); // 12
...
字符串的字符到索引映射