当前位置:K88软件开发文章中心编程语言.NETC# → 文章内容

C# 字符串(String)使用教程

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 14:00:19

oldChar,char newChar)把当前 string 对象中,所有指定的 Unicode 字符替换为另一个指定的 Unicode 字符,并返回新的字符串。28public string Replace(string oldValue,string newValue)把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。29public string[] Split(params char[] separator)返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。30public string[] Split(char[] separator,int count)返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。int 参数指定要返回的子字符串的最大数目。31public bool StartsWith(string value)判断字符串实例的开头是否匹配指定的字符串。32public char[] ToCharArray()返回一个带有当前 string 对象中所有字符的 Unicode 字符数组。33public char[] ToCharArray(int startIndex,int length)返回一个带有当前 string 对象中所有字符的 Unicode 字符数组,从指定的索引开始,直到指定的长度为止。34public string ToLower()把字符串转换为小写并返回。35public string ToUpper()把字符串转换为大写并返回。36public string Trim()移除当前 String 对象中的所有前导空白字符和后置空白字符。上面的方法列表并不详尽,请访问 MSDN 库,查看完整的方法列表和 String 类构造函数。实例下面的实例演示了上面提到的一些方法:比较字符串using System;namespace StringApplication{ class StringProg { static void Main(string[] args) { string str1 = "This is test"; string str2 = "This is text"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal."); } Console.ReadKey() ; } }}当上面的代码被编译和执行时,它会产生下列结果:This is test and This is text are not equal.字符串包含字符串:using System;namespace StringApplication{ class StringProg { static void Main(string[] args) { string str = "This is test"; if (str.Contains("test")) { Console.WriteLine("The sequence 'test' was found."); } Console.ReadKey() ; } }}当上面的代码被编译和执行时,它会产生下列结果:The sequence 'test' was found.获取子字符串:using System; namespace StringApplication {class StringProg { static void Main(string[] args) { string str = "Last night I dreamt of San Pedro"; Console.WriteLine(str); string substr = str.Substring(23); Console.WriteLine(substr); Console.ReadKey() ; } } }运行实例 ?当上面的代码被编译和执行时,它会产生下列结果:Last night I dreamt of San PedroSan Pedro连接字符串:using System;namespace StringApplication{ class StringProg { static void Main(string[] args) { string[] starray = new string[]{"Down the way nights are dark", "And the sun shines daily on the mountain top", "I took a trip on a sailing ship", "And when I reached Jamaica", "I made a stop"}; string str = String.Join("\n", starray); Console.WriteLine(str); Console.ReadKey() ; } }}当上面的代码被编译和执行时,它会产生下列结果:Down the way nights are darkAnd the sun shines daily on the mountain topI took a trip on a sailing shipAnd when I reached JamaicaI made a stop

上一页  [1] [2] 


C# 字符串(String)使用教程