C#字符串要点(复习专用)

一、字符串

  •   通过string定义一个字符串,或者通过String类来创建对象。
    •   通过new String() 创建有一下几种构造函数(从元数据),以此顺序创建string;
    • //
              // 摘要:
              //     将 System.String 类的新实例初始化为由 Unicode 字符数组指示的值。
              //
              // 参数:
              //   value:
              //     Unicode 字符的数组。
              [SecuritySafeCritical]
              public String(char[] value);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由指向 8 位有符号整数数组的指针指示的值。
              //
              // 参数:
              //   value:
              //     一个指针,指向以 null 结尾的 8 位带符号整数数组。
              //
              // 异常:
              //   T:System.ArgumentNullException:
              //     value 为 null。
              //
              //   T:System.ArgumentException:
              //     如果 value 是用 ANSI 编码的,则不能使用 value 初始化 System.String 的新实例。
              //
              //   T:System.ArgumentOutOfRangeException:
              //     要初始化的新字符串的长度过大,无法分配,该长度由 value 的 null 终止字符决定。
              //
              //   T:System.AccessViolationException:
              //     value 指定的地址无效。
              [CLSCompliant(false)]
              [SecurityCritical]
              public String(sbyte* value);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由指向 Unicode 字符数组的指定指针指示的值。
              //
              // 参数:
              //   value:
              //     指向以 null 终止的 Unicode 字符数组的指针。
              //
              // 异常:
              //   T:System.ArgumentOutOfRangeException:
              //     当前进程并不是对所有编址字符都有读取权限。
              //
              //   T:System.ArgumentException:
              //     value 指定了包含无效 Unicode 字符的数组,或者 value 指定了小于 64000 的地址。
              [CLSCompliant(false)]
              [SecurityCritical]
              public String(char* value);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由重复指定次数的指定 Unicode 字符指示的值。
              //
              // 参数:
              //   c:
              //     一个 Unicode 字符。
              //
              //   count:
              //     c 出现的次数。
              //
              // 异常:
              //   T:System.ArgumentOutOfRangeException:
              //     count 小于零。
              [SecuritySafeCritical]
              public String(char c, int count);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由 Unicode 字符数组、该数组内的起始字符位置和一个长度指示的值。
              //
              // 参数:
              //   value:
              //     Unicode 字符的数组。
              //
              //   startIndex:
              //     value 内的起始位置。
              //
              //   length:
              //     要使用的 value 内的字符数。
              //
              // 异常:
              //   T:System.ArgumentNullException:
              //     value 为 null。
              //
              //   T:System.ArgumentOutOfRangeException:
              //     startIndex 或 length 小于零。- 或 -startIndex 和 length 之和大于 value 中的元素数。
              [SecuritySafeCritical]
              public String(char[] value, int startIndex, int length);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由指向 8 位有符号整数数组的指定指针、该数组内的起始位置和一个长度指示的值。
              //
              // 参数:
              //   value:
              //     指向 8 位带符号整数数组的指针。
              //
              //   startIndex:
              //     value 内的起始位置。
              //
              //   length:
              //     要使用的 value 内的字符数。
              //
              // 异常:
              //   T:System.ArgumentNullException:
              //     value 为 null。
              //
              //   T:System.ArgumentOutOfRangeException:
              //     startIndex 或 length 小于零。- 或 -由 value + startIndex 指定的地址相对于当前平台来说太大;即,地址计算溢出。-
              //     或 -要初始化的新字符串的长度太大,无法分配。
              //
              //   T:System.ArgumentException:
              //     由 value + startIndex 指定的地址小于 64K。- 或 -如果 value 是用 ANSI 编码的,则不能使用 value 初始化 System.String
              //     的新实例。
              //
              //   T:System.AccessViolationException:
              //     value、startIndex 和 length 共同指定的地址无效。
              [CLSCompliant(false)]
              [SecurityCritical]
              public String(sbyte* value, int startIndex, int length);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由指向 Unicode 字符数组的指定指针、该数组内的起始字符位置和一个长度指示的值。
              //
              // 参数:
              //   value:
              //     指向 Unicode 字符数组的指针。
              //
              //   startIndex:
              //     value 内的起始位置。
              //
              //   length:
              //     要使用的 value 内的字符数。
              //
              // 异常:
              //   T:System.ArgumentOutOfRangeException:
              //     startIndex 或 length 小于零,value + startIndex 引起指针溢出,或者当前进程并不是对所有编址字符都有读取权限。
              //
              //   T:System.ArgumentException:
              //     value 指定了包含无效 Unicode 字符的数组,或者 value + startIndex 指定了小于 64000 的地址。
              [CLSCompliant(false)]
              [SecurityCritical]
              public String(char* value, int startIndex, int length);
              //
              // 摘要:
              //     将 System.String 类的新实例初始化为由指向 8 位有符号整数数组的指定指针、该数组内的起始位置、长度以及 System.Text.Encoding
              //     对象指示的值。
              //
              // 参数:
              //   value:
              //     指向 8 位带符号整数数组的指针。
              //
              //   startIndex:
              //     value 内的起始位置。
              //
              //   length:
              //     要使用的 value 内的字符数。
              //
              //   enc:
              //     一个对象,用于指定如何对 value 所引用的数组进行编码。如果 enc 为 null,则假定以 ANSI 编码。
              //
              // 异常:
              //   T:System.ArgumentNullException:
              //     value 为 null。
              //
              //   T:System.ArgumentOutOfRangeException:
              //     startIndex 或 length 小于零。- 或 -由 value + startIndex 指定的地址相对于当前平台来说太大;即,地址计算溢出。-
              //     或 -要初始化的新字符串的长度太大,无法分配。
              //
              //   T:System.ArgumentException:
              //     由 value + startIndex 指定的地址小于 64K。- 或 -未能使用 value 初始化 System.String 的新实例,假定 value
              //     按照 enc 的指定进行编码。
              //
              //   T:System.AccessViolationException:
              //     value、startIndex 和 length 共同指定的地址无效。
              [CLSCompliant(false)]
              [SecurityCritical]
              public String(sbyte* value, int startIndex, int length, Encoding enc);
      View Code
    • char []cstr = { 'a','b','c','d','e'};
                  char cstr1 = 'a';
                  sbyte se = 113;
                
                  String a = new String(cstr);
                  //
                
                  String b = new String(&cstr1);
              
                  String c = new String(&se);
          
                  String d = new String('d',2);
            
                  String e = new String(cstr,2,3);
      
                  String f = new String(&se,0,1);
                  Console.WriteLine((int)Convert.ToChar(f)+"\n");//113
                  String g = new String(&se,0,1,Encoding.UTF8);
                  Console.WriteLine("a:{0}\nb:{1}\nc:{2}\nd:{3}\ne:{4}\nf:{5}\ng:{6}",a,b,c,d,e,f,g);
      View Code
  •   string是C#基元类型(primitive),string简单来说就是编译器直接支持的数据类型。运算符(== 和 !=)是为了比较 string 对象的值, 而不是比较引用
    • string a = "1234";
                  string b = "123";
                  b += "4";
                  Console.WriteLine(a == b);//比较值
                  Console.WriteLine((object)a == (object)b);//比较引用()
  •   string连接操作 (+=)
    •   缺点很明显, 操作频繁的话十分浪费内存空间的
    •   使用StringBuilder类对象方法append代替+=, 性能还有所提升
  •   null," "和string.Empty的区别
    •   null表示不引用任何变量的空引用的值,null是引用变量的默认值, 值类型变量无法使用
    •   ""表示分配一个内存为空的存储空间。
    •   string.Empty表示空字符串,并且不分配内存空间。
  •   判断字符串为空 的俩种方法
    •   x.length==0 // 无法判断为null的字符串
    •   string.IsNullOrEmpty(x)
       

猜你喜欢

转载自www.cnblogs.com/hkf100/p/9564495.html