C# 把String 说透

String.Split 方法有6个重载函数:

1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)

下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):

1. public string[] Split(params char[] separator)
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}

2. public string[] Split(char[] separator, int count)
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}

3. public string[] Split(char[] separator, StringSplitOptions options)
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

4. public string[] Split(string[] separator, StringSplitOptions options)
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

5. public string[] Split(char[] separator, int count, StringSplitOptions options)
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

6. public string[] Split(string[] separator, int count, StringSplitOptions options)
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

as、Convert和强类型转换的比较

Convert.ToString((object)null) == ""
Convert.ToString(null)== null
通常 object 到 string 有四种方式(假设有object obj):obj.ToString()、Convert.ToString()

、(string)obj、obj as string。他们都能将 object 对象转换成 string 对象。我就讲讲他们的

异同以及在实际中应该使用哪个。

前两个方法通常是由别的对象得到 string 对象,它们间的区别只表现在要转换的对象为 null 时

,如果 obj 为 null,调用 obj.ToString 方法会导致 NullReferenceException 异常,调用

Convert.ToString 不会抛出异常而返回一个 null。

用强制转换 (string)obj 要求 obj 的运行时类型必须是 string。如果不是,就会抛出异常。

用 as 方法则会相对平稳,当 obj 的运行时类型不是 string 时会返回 null 而不抛出异常。

所以在通常在我们需要得到某个对象的 string 表达形式时,我们应该使用 ToString 和

Convert.ToString,这时候你就得根据情形选一个,假如你能保证你的对象不为 null,则两个差不

多。如果有可能为 null,你就应该用 Convert.ToString,如果你希望它为 null 的时候抛出异常

,那么当然可以选择 ToString。

string 的方法:

  名称 说明
Public methodSupported by the .NET Compact Framework Clone 返回对此 String 实例的引用。
Public methodStaticSupported by the .NET Compact Framework Compare 已重载。 比较两个指定的 String 对象。
Public methodStatic CompareOrdinal 已重载。 通过计算每个字符串中相应 Char 对象的数值来比较两个 String 对象。
Public method CompareTo 已重载。 将此实例与指定的对象或 String 进行比较,并返回二者相对值的指示。
Public methodStaticSupported by the .NET Compact Framework Concat 已重载。 连接 String 的一个或多个实例,或 Object 的一个或多个实例的值的 String 表示形式。
Public method Contains 返回一个值,该值指示指定的 String 对象是否出现在此字符串中。
Public methodStaticSupported by the .NET Compact Framework Copy 创建一个与指定的 String 具有相同值的 String 的新实例。
Public methodSupported by the .NET Compact Framework CopyTo 将指定数目的字符从此实例中的指定位置复制到 Unicode 字符数组中的指定位置。
Public methodSupported by the .NET Compact Framework EndsWith 已重载。 确定 String 的实例的末尾是否与指定的字符串匹配。
Public methodSupported by the .NET Compact Framework Equals 已重载。 已重写。 确定两个 String 对象是否具有相同的值。
Public methodStaticSupported by the .NET Compact Framework Format 已重载。 将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。
Public method GetEnumerator 检索一个可以循环访问此字符串中的每个字符的对象。
Public methodSupported by the .NET Compact Framework GetHashCode 已重写。 返回该字符串的哈希代码。
Public methodSupported by the .NET Compact Framework GetType  获取当前实例的 Type。 (从 Object 继承。)
Public methodSupported by the .NET Compact Framework GetTypeCode 返回类 StringTypeCode
Public methodSupported by the .NET Compact Framework IndexOf 已重载。 报告 String 或一个或多个字符在此字符串中的第一个匹配项的索引。
Public methodSupported by the .NET Compact Framework IndexOfAny 已重载。 报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的索引。
Public methodSupported by the .NET Compact Framework Insert 在此实例中的指定索引位置插入一个指定的 String 实例。
Public methodStaticSupported by the .NET Compact Framework Intern 检索系统对指定 String 的引用。
Public methodStaticSupported by the .NET Compact Framework IsInterned 检索对指定 String 的引用。
Public method IsNormalized 已重载。 指示此字符串是否符合特定的 Unicode 范式。
Public methodStaticSupported by the .NET Compact Framework IsNullOrEmpty 指示指定的 String 对象是 空引用(在 Visual Basic 中为 Nothing) 还是 Empty 字符串。
Public methodStaticSupported by the .NET Compact Framework Join 已重载。 在指定 String 数组的每个元素之间串联指定的分隔符 String,从而产生单个串联的字符串。
Public methodSupported by the .NET Compact Framework LastIndexOf 已重载。 报告指定的 Unicode 字符或 String 在此实例中的最后一个匹配项的索引位置。
Public methodSupported by the .NET Compact Framework LastIndexOfAny 已重载。 报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的索引位置。
Public method Normalize 已重载。 返回一个新字符串,其二进制表示形式符合特定的 Unicode 范式。
Public methodStaticSupported by the .NET Compact Framework op_Equality 确定两个指定的 String 对象是否具有同一值。
Public methodStaticSupported by the .NET Compact Framework op_Inequality 确定两个指定的 String 对象是否具有不同的值。
Public methodSupported by the .NET Compact Framework PadLeft 已重载。 右对齐此实例中的字符,在左边用空格或指定的 Unicode 字符填充以达到指定的总长度。
Public methodSupported by the .NET Compact Framework PadRight 已重载。 左对齐此字符串中的字符,在右边用空格或指定的 Unicode 字符填充以达到指定的总长度。
Public methodStaticSupported by the .NET Compact Framework ReferenceEquals  确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
Public methodSupported by the .NET Compact Framework Remove 已重载。 从此实例中删除指定个数的字符。
Public methodSupported by the .NET Compact Framework Replace 已重载。 将此实例中的指定 Unicode 字符或 String 的所有匹配项替换为其他指定的 Unicode 字符或 String
Public methodSupported by the .NET Compact Framework Split 已重载。 返回包含此实例中的子字符串(由指定 CharString 数组的元素分隔)的 String 数组。
Public methodSupported by the .NET Compact Framework StartsWith 已重载。 确定 String 实例的开头是否与指定的字符串匹配。
Public methodSupported by the .NET Compact Framework Substring 已重载。 从此实例检索子字符串。
Public methodSupported by the .NET Compact Framework ToCharArray 已重载。 将此实例中的字符复制到 Unicode 字符数组。
Public methodSupported by the .NET Compact Framework ToLower 已重载。 返回此 String 转换为小写形式的副本。
Public method ToLowerInvariant 返回此 String 对象的转换为小写形式的副本,返回时使用固定区域性的大小写规则。
Public methodSupported by the .NET Compact Framework ToString 已重载。 已重写。 将此实例的值转换为 String
Public methodSupported by the .NET Compact Framework ToUpper 已重载。 返回此 String 转换为大写形式的副本。
Public methodSupported by the .NET Compact Framework ToUpperInvariant 返回此 String 对象的转换为大写形式的副本,返回时使用固定区域性的大小写规则。
Public methodSupported by the .NET Compact Framework Trim 已重载。 从此实例的开始位置和末尾移除一组指定字符的所有匹配项。
Public methodSupported by the .NET Compact Framework TrimEnd 从此实例的结尾移除数组中指定的一组字符的所有匹配项。
Public methodSupported by the .NET Compact Framework TrimStart 从此实例的开始位置移除数组中指定的一组字符的所有匹配项。

string 类型处理:

string 类型表示一个字符序列(零个或更多 Unicode 字符)。 string 是 .NET Framework 中 String 的别名。

尽管 string 是引用类型,但定义相等运算符(==!=)是为了比较 string 对象(而不是引用)的值。 这使得对字符串相等性的测试更为直观。 例如:

string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);

这将先显示“True”,然后显示“False”,因为字符串的内容是相同的,但是 ab 引用的不是同一个字符串实例。

+ 运算符用于连接字符串:

string a = "good " + "morning";

这将创建一个包含“good morning”的字符串对象。

字符串是不可变的,即:字符串对象在创建后,尽管从语法上看您似乎可以更改其内容,但事实上并不可行。 例如,编写此代码时,编译器实际上会创建一个新字符串对象来保存新的字符序列,且新对象将赋给 b。 然后字符串“h”将适宜于垃圾回收。

string b = "h";
b += "ello";

[] 运算符可以用于对 string 的各个字符的只读访问。

string str = "test";
char x = str[2];  // x = 's';

字符串为 string 类型并可写成两种形式,即用引号引起来和用 @ 引起来。 用引号引起来的字符串括在双引号 (") 内:

"good morning" // a string literal

字符串文本可包含任何字符。 包括转义序列。 下面的示例使用转义序列 \\ 来表示反斜杠,使用 \u0066 来表示字母 f,使用 \n 来表示换行符。

 
       
string a = "\\\u0066\n";
Console.WriteLine(a);
注意注意

转义码 \udddd(其中 dddd 是一个四位数)表示 Unicode 字符 U+dddd 此外识别 8 位数字的 Unicode 转义码: \Udddddddd

原义字符串以 @ 开头并且也用双引号引起来。 例如:

@"good morning"  // a string literal
原义字符串的优势在于 处理转义序列,因此很容易写入,例如完全限定的文件名就是原义字符串:
 
       
@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"

若要在一个用 @ 引起来的字符串中包括一个双引号,请使用两对双引号:

@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.

@ 符号的另一种用法是使用作为 C# 关键字的被引用的 (/reference) 标识符。

有关 C# 中字符串的更多信息,请参见字符串(C# 编程指南)

以上来自MSDN.

我们常用的有:

   string test = "cbdddd";

 String.Format("abcd{0},{1}","ddd","bbb");//替换{0},{1}

  String.IsNullOrEmpty("  "); //返回True;如果不是 返回false;

  test.Index0f("a");//如果有返回>=0常数;如果没有返回-1;

  test.Replace("c","abc");//替换,

 test.LastIndexOf("d");  //返回最后一个含d的位置这里是5

转载于:https://www.cnblogs.com/springyangwc/archive/2011/03/14/1983805.html

猜你喜欢

转载自blog.csdn.net/weixin_34343308/article/details/94228101
今日推荐