在字符串连接的
string 关键字声明的一个字符数组,string s = "Hello, World!";
连接字符串 考虑性能的情况下 当然最好使用StringBuilder
string的连接字符串
string s1 = "orange";
string s2 = "red";
s1 += s2;
System.Console.WriteLine(s1);
// 输出 "orangered"
string s1 = "orange";
string s2 = "red";
s1 += s2;
System.Console.WriteLine(s1);
// 输出 "orangered"
stringBuilder的连接字符串
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("one ");
sb.Append("two ");
sb.Append("three");
string str = sb.ToString();
System.Console.WriteLine(str);
//输出 onetwothree
字符串中可以包含转义符,如“\n”(新行)和“\t”(制表符),
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("one ");
sb.Append("two ");
sb.Append("three");
string str = sb.ToString();
System.Console.WriteLine(str);
//输出 onetwothree
有个特殊的符号@
@ 符号会告知字符串构造函数忽略转义符和分行符,
如下:
相同的结果
string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";
在string 中常用的方法有几个
string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";
1.Split
此方法返回一个字符串数组,其中每个元素是一个单词
split
class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);
string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}
输出的结果为:
class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);
string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}
Original text: 'one two three:four,five six seven' 7 words in text: one two three four five six seven
2.搜索字符串
、、 和 方法
搜索字符串
class StringSearch
{
static void Main()
{
string str = "A silly sentence used for silly purposes.";
System.Console.WriteLine("'{0}'",str);
bool test1 = str.StartsWith("a silly");
System.Console.WriteLine("starts with 'a silly'? {0}", test1);
bool test2 = str.StartsWith("a silly", System.StringComparison.OrdinalIgnoreCase);
System.Console.WriteLine("starts with 'a silly'? {0} (ignoring case)", test2);
bool test3 = str.EndsWith(".");
System.Console.WriteLine("ends with '.'? {0}", test3);
int first = str.IndexOf("silly");
int last = str.LastIndexOf("silly");
string str2 = str.Substring(first, last - first);
System.Console.WriteLine("between two 'silly' words: '{0}'", str2);
}
}
输出:
'A silly sentence used for silly purposes.'
starts with 'a silly'? False
starts with 'a silly'? True (ignore case)
ends with '.'? True
between two 'silly' words: 'silly sentence used for '
class StringSearch
{
static void Main()
{
string str = "A silly sentence used for silly purposes.";
System.Console.WriteLine("'{0}'",str);
bool test1 = str.StartsWith("a silly");
System.Console.WriteLine("starts with 'a silly'? {0}", test1);
bool test2 = str.StartsWith("a silly", System.StringComparison.OrdinalIgnoreCase);
System.Console.WriteLine("starts with 'a silly'? {0} (ignoring case)", test2);
bool test3 = str.EndsWith(".");
System.Console.WriteLine("ends with '.'? {0}", test3);
int first = str.IndexOf("silly");
int last = str.LastIndexOf("silly");
string str2 = str.Substring(first, last - first);
System.Console.WriteLine("between two 'silly' words: '{0}'", str2);
}
}
输出:
'A silly sentence used for silly purposes.'
starts with 'a silly'? False
starts with 'a silly'? True (ignore case)
ends with '.'? True
between two 'silly' words: 'silly sentence used for '
3.字符串的修改
方法
字符串是不可变的,因此不能修改字符串的内容。但是,可以将字符串的内容提取到非不可变的窗体中,并对其进行修改,以形成新的字符串实例
方法来将字符串的内容提取到 char 类型的数组中。然后修改此数组中的某些元素。之后,使用 char 数组创建新的字符串实例
字符串修改
class ModifyStrings
{
static void Main()
{
string str = "The quick brown fox jumped over the fence";
System.Console.WriteLine(str);
char[] chars = str.ToCharArray();
int animalIndex = str.IndexOf("fox");
if (animalIndex != -1)
{
chars[animalIndex++] = 'c';
chars[animalIndex++] = 'a';
chars[animalIndex] = 't';
}
string str2 = new string(chars);
System.Console.WriteLine(str2);
}
}
class ModifyStrings
{
static void Main()
{
string str = "The quick brown fox jumped over the fence";
System.Console.WriteLine(str);
char[] chars = str.ToCharArray();
int animalIndex = str.IndexOf("fox");
if (animalIndex != -1)
{
chars[animalIndex++] = 'c';
chars[animalIndex++] = 'a';
chars[animalIndex] = 't';
}
string str2 = new string(chars);
System.Console.WriteLine(str2);
}
}
附录:转义字符
转义字符
一般字符
除 .$ ^ { [ ( | ) * + ? \ 外,其他字符与自身匹配。
\a
与响铃(警报)\u0007 匹配。
\b
如果在 [] 字符类中,则与退格符 \u0008 匹配;如果不是这种情况,请参见本表后面的“注意”部分。
\t
与 Tab 符 \u0009 匹配。
\r
与回车符 \u000D 匹配。
\v
与垂直 Tab 符 \u000B 匹配。
\f
与换页符 \u000C 匹配。
\n
与换行符 \u000A 匹配。
\e
与 Esc 符 \u001B 匹配。
\040
将 ASCII 字符匹配为八进制数(最多三位);如果没有前导零的数字只有一位数或者与捕获组号相对应,则该数字为后向引用。(有关更多信息,请参见反向引用。)例如,字符 \040 表示空格。
\x20
使用十六进制表示形式(恰好两位)与 ASCII 字符匹配。
\cC
与 ASCII 控制字符匹配;例如,\cC 为 Ctrl-C。
\u0020
使用十六进制表示形式(恰好四位)与 Unicode 字符匹配。
注意
.NET Framework 不支持用于指定 Unicode 的 Perl 5 字符转义。Perl 5 字符转义的格式是 \x{####…},其中“####…”是十六进制数字的序列。应改为使用本行中描述的 .NET Framework 字符转义。
\
在后面带有不识别为转义符的字符时,与该字符匹配。例如,\* 与 \x2A 相同。
注意
转义字符 \b 是一个特例。在正则表达式中,\b 表示单词边界(在 \w 和 \W 之间),不过,在 [] 字符类中,\b 表示退格符。在替换模式中,\b 始终表示退格符。
一般字符
除 .$ ^ { [ ( | ) * + ? \ 外,其他字符与自身匹配。
\a
与响铃(警报)\u0007 匹配。
\b
如果在 [] 字符类中,则与退格符 \u0008 匹配;如果不是这种情况,请参见本表后面的“注意”部分。
\t
与 Tab 符 \u0009 匹配。
\r
与回车符 \u000D 匹配。
\v
与垂直 Tab 符 \u000B 匹配。
\f
与换页符 \u000C 匹配。
\n
与换行符 \u000A 匹配。
\e
与 Esc 符 \u001B 匹配。
\040
将 ASCII 字符匹配为八进制数(最多三位);如果没有前导零的数字只有一位数或者与捕获组号相对应,则该数字为后向引用。(有关更多信息,请参见反向引用。)例如,字符 \040 表示空格。
\x20
使用十六进制表示形式(恰好两位)与 ASCII 字符匹配。
\cC
与 ASCII 控制字符匹配;例如,\cC 为 Ctrl-C。
\u0020
使用十六进制表示形式(恰好四位)与 Unicode 字符匹配。
注意
.NET Framework 不支持用于指定 Unicode 的 Perl 5 字符转义。Perl 5 字符转义的格式是 \x{####…},其中“####…”是十六进制数字的序列。应改为使用本行中描述的 .NET Framework 字符转义。
\
在后面带有不识别为转义符的字符时,与该字符匹配。例如,\* 与 \x2A 相同。
注意
转义字符 \b 是一个特例。在正则表达式中,\b 表示单词边界(在 \w 和 \W 之间),不过,在 [] 字符类中,\b 表示退格符。在替换模式中,\b 始终表示退格符。
关于为什么用StringBuilder会比string连接字符串效率高,会在C#分析目录写出来,当然那都是个人意见,不一定正解,
希望高人能够知道.