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!

更新於:2019 年 12 月 2 日

8K+ 瀏覽

開啟你的事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.