时间:2022-08-11 11:28:47 | 栏目:.NET代码 | 点击:次
string本身可看作一个Char数组。
string s = "hello world"; for (int i = 0; i < s.Length; i++) { Console.WriteLine(s[i]); } //或者 foreach (char c in s) { Console.WriteLine(c); }
打散为字符数组(ToCharArray)
string s = "Hello World"; char[] arr = s.ToCharArray(); // Console.WriteLine(arr[0]); // 输出数组的第一个元素,输出"H"
string s = "hello world"; Console.WriteLine(s.Substring(3));//lo world Console.WriteLine(s.Substring(3, 4));//lo w
string s = "hello world"; Console.WriteLine(s.IndexOf("o"));//4 Console.WriteLine(s.LastIndexOf("o"));//7 Console.WriteLine(s.IndexOf('l'));//查找该字符串中的第一次'l'出现位置 2 Console.WriteLine(s.IndexOf('l', 4));//查找该字符串中的第四次'l'出现位置 9 Console.WriteLine(s.IndexOf('l', 5, 6)); //从前向后定位从第5位开始和再数6位位置之间'l'出现的位置; 9 Console.WriteLine(s.Contains("e"));//True
string s = "hello world"; Console.WriteLine(s.PadLeft(15, '*'));//****hello world Console.WriteLine(s.PadRight(15, '*'));//hello world****
string s = "Hello World"; Console.WriteLine(s.ToUpper());//HELLO WORLD Console.WriteLine(s.ToLower());//hello world
string s = "Hello World"; Console.WriteLine(s.Remove(7));//Hello W Console.WriteLine(s.Remove(7,1));//Hello Wrld
string s = "Hello World"; Console.WriteLine(s.Replace("l","*"));//He**o Wor*d Console.WriteLine(s.Replace("or","*"));//Hello W*ld
string s = " Hello World "; Console.WriteLine(s.Trim());//"Hello World" Console.WriteLine(s.TrimStart());// "Hello World " Console.WriteLine(s.TrimEnd());// " Hello World" string s1 = "hello Worldhd"; Console.WriteLine(s1.Trim('h','d'));//ello Worl Console.WriteLine(s1.TrimStart('h','d'));//ello Worldhd Console.WriteLine(s1.TrimEnd('h','d'));//hello Worl
string s = "Hello World"; Console.WriteLine(s.Insert(6,"测试"));//Hello 测试World
string s = "Hello Wor ld "; Console.WriteLine(s.Split('o'));//"Hell","W", "r ld " Console.WriteLine(s.Split('e', 'd'));// "H","llo Wor l"," " Console.WriteLine(s.Split(new string[] { " " }, StringSplitOptions.None));//"Hello","Wor","","ld"," " Console.WriteLine(s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));//"Hello","Wor","ld" foreach (string sub in s.Split('o')) { Console.WriteLine(sub); }
1、String.Concat(str1, str2, …., strn):将n个字符串连接,中间没有连接符。字符串连接也可以用 ‘+’ 来实现。
2、将字符串列表(string[],IEnumerable<string>)用str将各项串连起来,静态函数Join(SplitCh, array)
string[] arr = {"Hello”,” World"}; Console.WriteLine(string.Join("*", arr));//Hello*World
3、实例方法StringBuilder.Append
StringBuilder sb =new StringBuilder(); // 声明一个字符串构造器实例 sb.Append("A"); // 使用字符串构造器连接字符串能获得更高的性能 sb.Append('B'); Console.WriteLine(sb.ToString());// 输出"AB"
string s = "Hello World"; Console.WriteLine(string.Format("I Love \"{0}\"", s));//I Love "Hello World" Console.WriteLine(string.Format("{0:C}", 120.2));//?120.20 Console.WriteLine(string.Format("{0:yyyy年MM月dd日}", DateTime.Now));
1、Int32.TryParse(string,out bool ):始终不抛异常,返回true/false来说明是否成功解析。string为null返回解析不成功。
int ret = 0; if (int.TryParse("120.5", out ret))//转换异常! { Console.WriteLine(ret); } else { Console.WriteLine("转换异常!"); }
2、Int32.Parse(string):解析不成功会抛出异常。string为null时抛出“值不能为 null。参数名: String”异常。
try { Console.WriteLine(int.Parse("120.5"));//输入字符串的格式不正确 } catch (Exception e) { Console.WriteLine(e.Message); }
3、Convert.ToInt32(string):解析不成功会抛出异常,但是string为null时不抛异常返回0.
try { Console.WriteLine(Convert.ToInt32("120.5"));//输入字符串的格式不正确。 } catch (Exception e) { Console.WriteLine(e.Message); }
1、Compare()是CompareTo()的静态版本,返回小于,大于还是等于后者字符串(分别为-1,1,0)
string str1 = "you are very happy!!"; string str2 = "I am very happy!!"; Console.WriteLine( string.Compare(str1, str2));//1 Console.WriteLine(str1.CompareTo(str2));//1
2、CompareOrdinal():对两字符串比较,而不考虑本地化语言和文化。将整个字符串每5个字符(10个字节)分成一组,然后逐个比较,找到第一个不相同的ASCII码后退出循环。并且求出两者的ASCII码的差。
string str1 = "you are very happy!!"; string str2 = "I am very happy!!"; Console.WriteLine(string.CompareOrdinal(str1, str2));//48
3、Equals()与“==”等价,静态或实例Equals,返回相等还是不等(true/false).
string str2 = "I am very happy!!"; string str3 = "I am very happy!!"; Console.WriteLine(str2.Equals(str3));//True Console.WriteLine(str2 == str3);//True
(1)、String.Copy(str):参数str为要复制的字符串,它回返回一个与该字符串相等的字符串
(2)、SreStr.CopyTo(StartOfSreStr, DestStr, StartOfDestStr, CopyLen):它必须被要复制的字符串实例调用,它可以实现复制其中某一部分到目标字符串的指定位置
string s = "Hello World"; string s1 = String.Copy(s); Console.WriteLine(s1);//Hello World char[] s2 = new char[20]; s.CopyTo(2, s2, 0, 8); Console.WriteLine(s2);//llo Worl000000000000000000000000
string s = "Hello World"; Console.WriteLine(s.StartsWith("He")); // True Console.WriteLine(s.EndsWith("He")); // False
string s = "Hello World"; string s2 = null; Console.WriteLine(string.IsNullOrEmpty(s)); // False Console.WriteLine(string.IsNullOrEmpty(s2)); // True