1.Compare 比较字符串
public static void Main() { string x = "nihao"; string y = "nihao ma";结果:-1 //2.string x = "nihao ma"; //string y = "nihao";结果: 1 //3.string x = "nihao"; //string y = "nihao";结果: 0 //4.string x = "niliu"; //string y = "nihao";结果: 1 int result = string.Compare(x,y); Console.WriteLine("结果:{0}",result); Console.ReadKey(); }string x="你好";string y=",欢迎你";string z=string.Concat(x,y);string dest = "Hello world";string source = "Goodbye China";char[] destArray = dest.ToCharArray();//将dest变成字符数组source.CopyTo(8, destArray, 6, 5);//从source的第8个字符起复制5个字符并从destArray的第6个位置开始放dest = new string(destArray);//这时dest为"Hello China"定位字符串:
在上述重载形式中,其参数含义如下:
Value:待定位的字符或者子串。
startIndex:在总串中开始搜索的起始位置。
Count:在总串中从起始位置开始搜索的字符数。
String str1 = "hello world"; String str2 = "abcd"; int x = str1.IndexOf("o"); Console.WriteLine("结果是{0}",x); String str1 = "hello world"; String str2 = "abcd"; int x = str1.IndexOf("o"); int y = str1.IndexOf("o",5); Console.WriteLine("结果是{0},定索引位置的索引结果是{1}",x,y); Console.ReadKey(); String str1 = "hello world or happy you "; int z = str1.IndexOf("o",10,4); Console.WriteLine("倒序索引结果{0}",z);String str1 = "hello world or happy you "; char[] b = { ‘e‘, ‘o‘, ‘l‘ }; int a = str1.IndexOfAny(b,5,15); Console.WriteLine("结果是{0}}",a);String str1 = "hello world or happy you "; String str2 = "abcd"; string str3 = str1.Insert(2, str2); Console.WriteLine("insert功能插入结果{0}",str3); string[] str5 = {"fsa","fasdf","fsaf"}; string str4 = string.Join("/", str5); Console.WriteLine("Join功能结果{0}", str4); string str1 = "hello world"; char str2=‘a‘; string str3 = str1.PadLeft(12,str2); Console.WriteLine("PadLeft功能插入结果{0}", str3);string str1 = "hello world"; char str2=‘a‘; string str3 = str1.PadRight(12,str2); Console.WriteLine("PadLeft功能插入结果{0}", str3);public string Replace(char oldChar, char newChar);
public string Replace(string oldValue, string newValue);
string str1 = "hello world"; str1 = str1.Replace("d", "d!"); Console.WriteLine("Replace功能插入结果{0}", str1); string str1 = "hello world";string[] str2 = str1.Split(‘w‘); string str1 = "hello world"; string str2 = str1.Split(‘w‘)[0];Console.WriteLine("分割数据{0}",str2);string str1 = "changed";string str4 = str1.Remove(1, 2); string s = "Hello C# World!"; string s1=s.Substring(3); Console.WriteLine(s1); string s = "Hello C# World!"; string s1=s.Substring(3,2); Console.WriteLine(s1);原文:http://www.cnblogs.com/myhomebo/p/5481307.html