Concat:
对当前字符串追加字符串。
string str = "hello";
char[] chars = str.Concat(new char[] { ' ', 'w', 'o', 'r', 'l', 'd' }).ToArray();
Console.WriteLine(chars);//打印:hello world
string result = string.Concat(str, " ", "world", " how are you?");//打印:hello world how are you?
Join:
用某字符串来拼接字符串数组,使之成为一个新的字符串。
string[] str1 = { "abc", "def" };
string result1 = string.Join("---", str1);
Console.WriteLine(result);
Console.WriteLine(result1);//打印:abc---def
Startwith,Endwith:
是否以此字符串开头或者结束,返回True或者False
Console.WriteLine(str.StartsWith("he", StringComparison.OrdinalIgnoreCase));//返回true
Console.WriteLine(str.EndsWith("he", StringComparison.OrdinalIgnoreCase));//返回false
Format:
格式化,将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。
string name = "jack";
int age = 19;
string greeting = string.Format($"hello,I am {name},I 'm {age} this year");
Console.WriteLine(greeting);//打印:hello,I am jack,I 'm 19 this year
Padleft,Padright:
返回一个新字符串,
该字符串通过在此实例中的字符左侧填充
指定的 Unicode 字符来达到指定的总长度,
从而使这些字符右对齐。
即为:不足宽度补字符
Console.WriteLine(str.PadLeft(10));
Console.WriteLine(str.PadLeft(10, 'a'));
Console.WriteLine(str.PadRight(10));
Console.WriteLine(str.PadRight(10, 'a'));
/*输出结果:
* hello
aaaaahello
hello
helloaaaaa
*/
Trim:
从当前字符串删除所有前导空白字符和尾随空白字符
Console.WriteLine("--- trim ----");
string str6 = " hello ";
Console.WriteLine(str6);
Console.WriteLine(str6.Trim());
Console.WriteLine(str6.TrimStart());
/*
hello
hello
hello
*/
Split:
拆分字符串
Console.WriteLine("--- split-----");
string str8 = "aaaaabccdccccdeeeeee";
string[] sub1 = str8.Split(new char[] { 'd' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var dd in sub1)
{
Console.WriteLine(dd);
}
/*
aaaaabcc
cccc
eeeeee
*/
split的6个重载详细说明:
Split(Char[], Int32, StringSplitOptions) | 基于指定的分隔字符和(可选)选项将字符串拆分为最大数量的子字符串。 |
Split(String[], StringSplitOptions) | 基于指定的分隔字符串和(可选)选项将字符串拆分为子字符串。 |
Split(Char[]) | 根据指定的分隔字符将字符串拆分为子字符串。 |
Split(Char[], Int32) | 根据指定的分隔符将字符串拆分为最大数量的子字符串。 |
Split(String[], Int32, StringSplitOptions) | 基于指定的分隔字符串和(可选)选项将字符串拆分为最大数量的子字符串。 |
Split(Char[], StringSplitOptions) | 根据指定的分隔字符和选项将字符串拆分为子字符串。 |