C#语法手册

字符串的常用语法


C# 中字符串(string)的特性,以及使用的基本语法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSGDemoCSG016
{
    //https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/strings/
    //https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/character-encoding-introduction


    //#csg016-01
    //一些Unicode概念
    //Character/字符,比如汉字“你”
    //Code Point/码点,汉字“你”的码点为 U+4F60
    //UTF-8,Unicode编码方式之一,“你”的UTF-8编码为 \xE4\xBD\xA0
    //UTF-16,Unicode编码方式之一,“你”的UTF-16编码为 \u4F60
    //UTF-32,Unicode编码方式之一,“你”的UTF-32编码为 \U00004F60 



    //#csg016-02
    //string的基本信息
    //string 是 char 的有序集合,实现了 System.Collections.Generic.IEnumerable<char>
    //string关键字与System.String是等效的。
    //string对象是“不可变的”:它们在创建后无法更改。
    //看起来是在修改字符串的所有 String 方法和 C# 运算符实际上都是在新的字符串对象中返回结果。
    //string对象在内存中最大大小为2GB,大约1000000000(10亿)个char.



    //#csg016-03
    //char基本信息
    //一个char代表一个UTF-16编码单位(两个字节),比如  "你" 的 utf-16编码 '\u4F60'
    //通常一个char可以表示一个字符,但是对于某些字符需要多个char来表示,此时 string.Length 会大于可读的字符数量。
    //比如字符“𤖉”需要两个char表示 \uD851\uDD89



    //#csg016-04
    //string,char和unicode
    //因为char代表一个UTF-16编码单位,那么自然string使用的字符编码即为UTF-16。
    //在.net core 中所使用的Unicode版本取决于所在的操作系统。

    public class DemoCode
    {



        public void F()
        {
            //#csg016-05
            //使用双引号声明一个字符串
            string strval = "this is a string";

            //#csg016-06
            //声明一个空字符串
            //空字符串并不是空对象,区别如下
            string strvalNull = null; //空字符串对象
            string strval1 = "";//空字符串指的是只服从不包含任何char对象,但对象并不为空
            string strval2 = string.Empty;
            //strval1 == strval2; //true


            //#csg016-07
            //使用@声明多行字符串
            string strlval3 = @"this is a very long string this is a very 
                   long string this is a very long string";


            //#csg016-08
            //逐字字符串/verbatim strings
            //使用@声明逐字符串,在编译时,逐字字符串被转换为普通字符串。即:转义符号也会被视为普通字符串
            string path = @"C:\files.txt";
            //上面的字符串普通字符串声明为
            string path2 = "C:\\files.txt";

        }


        public void F2()
        {
            //#csg016-09
            //使用内插格式字符串
            //在C#6.0及之后的版本可以使用$实现字符串内插
            var jh = (firstName: "Jupiter", lastName: "Hammon", born: 1711, published: 1761);
            Console.WriteLine($"{jh.firstName} {jh.lastName} was an African American poet born in {jh.born}.");
            Console.WriteLine($"He was first published in {jh.published} at the age of {jh.published - jh.born}.");
            Console.WriteLine($"He'd be over {Math.Round((2018d - jh.born) / 100d) * 100d} years old today.");


            //#csg016-10
            //使用 String.Format 格式字符串
            var pw = (firstName: "Phillis", lastName: "Wheatley", born: 1753, published: 1773);
            Console.WriteLine("{0} {1} was an African American poet born in {2}.", pw.firstName, pw.lastName, pw.born);
            Console.WriteLine("She was first published in {0} at the age of {1}.", pw.published, pw.published - pw.born);
            Console.WriteLine("She'd be over {0} years old today.", Math.Round((2018d - pw.born) / 100d) * 100d);


            //#csg016-11
            //使用StringBuilder 
            //.NET 中的字符串操作进行了高度的优化,在大多数情况下不会显著影响性能。
            //但是,在某些情况下(例如,执行数百次或数千次的紧密循环),字符串操作可能影响性能。
            //StringBuilder 类创建字符串缓冲区,用于在程序执行多个字符串操控时提升性能。
            StringBuilder sb = new StringBuilder("Rat: the ideal pet");
            sb[0] = 'C';
            Console.WriteLine(sb.ToString());
            Console.ReadLine();


        }


        public void F3()
        {
            //#csg016-12
            //char的声明,使用单引号声明char
            var c1 = 'a';
            var c2 = '你';
            //var c3 = '𤖉'; //若输入字符需要多个char表示,则会报错 CS1012:字符文本中的字符套多


        }

    }


    //#csg016-13
    //C#中字符串的转义
    //	\'	单引号                       0x0027
    //	\"	双引号                       0x0022
    //	\\	反斜杠                       0x005C
    //	\0	null                        0x0000
    //	\a	警报                         0x0007
    //	\b	Backspace                   0x0008
    //	\f	换页                         0x000C
    //	\n	换行                         0x000A
    //	\r	回车                         0x000D
    //	\t	水平制表符                    0x0009
    //	\v	垂直制表符                    0x000B
    //	\u	Unicode 转义序列(UTF-16)      \uHHHH(范围:0000 - FFFF;)
    //	\U	Unicode 转义序列(UTF-32)      \U00HHHHHH(范围:000000 - 10FFFF;)
    //	\x	与“\u”类似,不过长度可变        \xH[H][H][H](范围:0 - FFFF;)



    //#csg016-14
    //各个.NET 版本使用的 Unicode 标准
    //.NET Framework 1.1                                      Unicode 标准,版本 4.0.0
    //.NET Framework 2.0                                      Unicode 标准,版本 5.0.0
    //.NET Framework 3.5                                      Unicode 标准,版本 5.0.0
    //.NET Framework 4                                        Unicode 标准,版本 5.0.0
    //Windows 7 上的.NET Framework 4.5 及更高版本               Unicode 标准,版本 5.0.0
    //Windows 8 及更高版本系统上的.NET Framework 4.5 及更高版本    Unicode 标准,版本 6.3.0
    //.NET Core 和.NET 5+                                      取决于操作系统支持的 Unicode 版本。

}