1、String.Join()
创建一个List并对此List赋值,调用string.Join(string separator,string[] value)并输出结果
List<String> list = new List<string>();
list.Add("第一");
list.Add("第二");
list.Add("第三");
Console.WriteLine(string.Join(",",list));
2、String.Replace()
在上述代码的基础上增加以下代码,测试string.Replace(string,string)方法的运行效果。
string s1 = "";
s1 = string.Join(",", list.ToArray());
string s2 = s1.Replace(",", "','");
Console.WriteLine(s2);
string.Replace()方法的参数还包括以下几种重载:
Replace(char,char)
Replace(String,String,StringComparison)
Replace(String,String,Boolean,CultureInfo)
3、String.Compare()方法
compare()方法对字符串进行比较时,根据字典顺序进行比较,分为三种情况,例如对字符串s1和字符串s2进行比较:
- s1先于s2,返回值为-1
- s1与s2相同,返回值为0
- s2先于s1,返回值为1
string s1 = "coma";
string s2 = "comb";
int i = string.Compare(s1, s2);
Console.WriteLine("s1为:" + s1);
Console.WriteLine("s2为:" + s2);
Console.WriteLine("s1先于s2时:"+i);
s1 = "coma";
s2 = "coma";
i= string.Compare(s1, s2);
Console.WriteLine("s1为:" + s1);
Console.WriteLine("s2为:" + s2);
Console.WriteLine("s1与s2相同时:" + i);
s1 = "comb";
s2 = "coma";
i = string.Compare(s1, s2);
Console.WriteLine("s1为:" + s1);
Console.WriteLine("s2为:" + s2);
Console.WriteLine("s1与s2相同时:" + i);
4、String.Contains()方法
string.Contains()方法用来判断指定的字串是否出现在字符串中,返回值为bool类型。
string s = "comes";
bool a=s.Contains("come");
Console.WriteLine(a);
s = "comes";
a = s.Contains("comeaaa");
Console.WriteLine(a);
Contains()方法的格式为str.Contains(value);其中str为具体的字符串,value为要查询的包含的字串。
此方法也包含其他集中重载,如下所示:
Contains(Char)
Contains(Char,StringComparison)
Contains(String,StringComparison)
简单使用方法