c中string,C# 把String 说透

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

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# 编程指南)。
Tags:  c中string

延伸阅读

最新评论

发表评论