一、字符
在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发。
internal static class CharConvert { public static void Go() { Char c; Int32 n; // 使用C#转型技术实现,强制类型转换 c = (Char)65; Console.WriteLine(c); // 显示 "A" n = (Int32)c; Console.WriteLine(n); // 显示 "65" c = unchecked((Char)(65536 + 65)); Console.WriteLine(c); // 显示 "A" // 使用Convert进行转换 c = Convert.ToChar(65); Console.WriteLine(c); // 显示 "A" n = Convert.ToInt32(c); Console.WriteLine(n); // Displays "65" // 显示Convert的范围检查 try { c = Convert.ToChar(70000); // 对 16-bits 来说过大 Console.WriteLine(c); // 不知心 } catch (OverflowException) { Console.WriteLine("Can‘t convert 70000 to a Char."); } // 使用IConvertible进行转换 c = ((IConvertible)65).ToChar(null); Console.WriteLine(c); // 显示 "A" n = ((IConvertible)c).ToInt32(null); Console.WriteLine(n); // 显示 "65" } }
二、字符串
class Program { private static void Main(string[] args) { String s = "Hi"; Console.WriteLine(s); } }
编译上述代码,并检查它的IL,会看到一下内容:
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint .maxstack 1 .locals init ( [0] string str) L_0000: nop L_0001: ldstr "Hi" L_0006: stloc.0 L_0007: ldloc.0 L_0008: call void [mscorlib]System.Console::WriteLine(string) L_000d: nop L_000e: ret }
//包含回车符和换行符的字符串 String s ="Hi\r\nthere"
但是,一般不建议这么做。因为在不同的平台解释是不同的,推荐使用System.Environment中定义的NewLine属性。NewLine属性是依赖于平台的,他会一句底层平台返回恰当的字符串。
bool Equals (string value, StringComparison comparisonType) static bool Equals (string a, string b, StringComparison comparisonType) static int Compare (string strA, string strB, StringComparison comparisonType) static int Compare (String strA, String strB, bool ignoreCase, CultureInfo culture) static int Compare (string strA, string strB, CultureInfo culture, CompareOptions options) static int Compare (string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType) static int Compare (string strA, int indexA, string strB, int indexB, int length, CultureInfo culture, CompareOptions options) static int Compare (String strA, int indexA, String strB, int indexB, int length, bool ignoreCase, CultureInfo culture)
进行排序时应该总是执行区分大小写的比较。原因是假如只是大小写不同的两个字符串被视为相等,那么每次对它们进行派时许,它们都可能按照不同的顺序排列,从而造成用户的迷惑。
public enum StringComparison { //使用区域敏感排序规则和当前区域比较字符串。 CurrentCulture, //使用区域敏感排序规则、当前区域来比较字符串,同时忽略被比较字符串的大小写。 CurrentCultureIgnoreCase, //使用区域敏感排序规则和固定区域比较字符串。 InvariantCulture, //使用区域敏感排序规则、固定区域来比较字符串,同时忽略被比较字符串的大小写。 InvariantCultureIgnoreCase, //使用序号排序规则比较字符串。 Ordinal, //使用序号排序规则并忽略被比较字符串的大小写,对字符串进行比较。 OrdinalIgnoreCase }
另外,前面有两个方法要求传递一个CompareOptions参数。这个参数要获取有CompareOptions枚举类型定义的一个值:
public enum CompareOptions { None = 0, //指示字符串比较必须忽略大小写。 IgnoreCase = 1, //指示字符串比较必须忽略不占空间的组合字符,比如音调符号。 IgnoreNonSpace = 2, //指示字符串比较必须忽略符号,如空白字符、标点符号、货币符号、百分号、数学符号、“&”符等等 IgnoreSymbols = 4, //指示字符串比较必须忽略 Kana 类型 IgnoreKanaType = 8, //指示字符串比较必须忽略字符宽度 IgnoreWidth = 16, //指示字符串比较必须使用字符串排序算法。 StringSort = 0x20000000, //指示必须使用字符串的连续 Unicode UTF-16 编码值进行字符串比较(使用代码单元进行代码单元比较),这样可以提高比较速度,但不能区分区域性 Ordinal = 0x40000000, //字符串比较必须忽略大小写,然后执行序号比较。 OrdinalIgnoreCase = 0x10000000 }
接受一个CompareOptions实参的方法要求你必须显式传递一个语言文化。如果传递了Ordinal或OrdinalIgnoreCase 标志,这些Comoare方法会忽略指定的语言文化。
//检索系统对指定 System.String 的引用 public static string Intern(string str) //检索对指定 System.String 的引用 public static string IsInterned(string str)
第一个方法Intern获取一个String,获得它的哈希码,并在内部哈希表中检查是否有匹配的。如果存在一个完全相同的字符串,就返回对这个字符串已经存在的String对象的一个引用。如果不存在,就创建字符串的副本,将副本添加到内部哈希表中,并返回对这个副本的一个引用。
public static void Go() { String s1 = "Hello"; String s2 = "Hello"; Console.WriteLine(Object.ReferenceEquals(s1, s2));// ‘False‘ s1 = String.Intern(s1); s2 = String.Intern(s2); Console.WriteLine(Object.ReferenceEquals(s1, s2));// ‘True‘ }
原文:http://www.cnblogs.com/zxj159/p/3564400.html