C# Trim()方法
C# 中的 Trim() 方法用于返回一个新字符串,其中删除了当前字符串中指定字符集的所有前导和尾随出现。
语法
public string Trim (); public string Trim (params char[] trimChars);
上面,trimChars 参数是要删除的 Unicode 字符数组,或 null。
示例
using System; using System.Globalization; public class Demo { public static void Main(String[] args) { string str1 = " JackSparrow!"; string str2 = " @#$PQRSTUV!"; Console.WriteLine("String1 = "+str1); Console.WriteLine("String1 (after trim) = "+str1.Trim()); Console.WriteLine("String1 ToUpper = "+str1.ToUpper(new CultureInfo("en-US", false))); Console.WriteLine("String1 Substring from index4 = " + str1.Substring(2, 2)); Console.WriteLine("
String2 = "+str2); Console.WriteLine("String2 ToUpper = "+str2.ToUpper(new CultureInfo("en-US", false))); Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5)); } }
输出
String1 = JackSparrow! String1 (after trim) = JackSparrow! String1 ToUpper = JACKSPARROW! String1 Substring from index4 = String2 = @#$PQRSTUV! String2 ToUpper = @#$PQRSTUV! String2 Substring from index2 = @
示例
using System; public class Demo { public static void Main(String[] args) { string str = "JackSparrow!"; char[] arr = { 'J', 'a'}; Console.WriteLine("String = "+str); Console.WriteLine("String (after trim) = " + str.Trim(arr)); } }
输出
String = JackSparrow! String (after trim) = ckSparrow!
广告