一、案例代码
- static void Main()
- {
- // Create a Unicode String with 5 Greek Alpha characters
- String szGreekAlpha = new String('\u0319', 5);
- // Create a Unicode String with a Greek Omega character
- String szGreekOmega1 = new String(new char[] { '\u03A9', '\u03A9', '\u03A9' });
- String szGreekOmega = new String(new char[] { '\u03A9', '\u03A9', '\u03A9' }, 2, 1);
-
- /* 连接三个指定对象 */
- String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone());
- Console.WriteLine(szGreekAlpha);
- Console.WriteLine(szGreekOmega1);
- Console.WriteLine(szGreekOmega);
- // Examine the result
- Console.WriteLine(szGreekLetters);
-
- // The first index of Alpha
- int ialpha = szGreekLetters.IndexOf('\u0319');
- // The last index of Omega
- int iomega = szGreekLetters.LastIndexOf('\u03A9');
-
- Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha +
- " and Omega last appears at index " + iomega + " in this String.");
-
- Console.ReadKey();
- }
图1
二、主要关注点:
1、String构造函数中的Unicode字符的写法:
‘\u0319’,而不是’/u0319’;
2、String的三个构造函数的写法:
- new String('\u0319', 5);
创建5个’\u0319’到字符串;
- new String(new char[] { '\u03A9', '\u03A9', '\u03A9' });
将 System.String 类的新实例初始化为由 Unicode 字符数组指示的值。
- new String(new char[] { '\u03A9', '\u03A9', '\u03A9' }, 2, 1);
将 System.String 类的新实例初始化为由指向 Unicode 字符数组的指定指针、该数组内的起始字符位置和一个长度指示的值。即取数据的a[2]开始的长度为1的字符串赋值给String。
参考博客: