Java 程式用於從字串中刪除首尾空白


使用 Trim() 方法來從字串中刪除首尾空白。假設我們在字串的最開始和最後都存在空白。

String str = " a ";

現在,使用 trim() 方法。

str.trim()

這以下是最終的示例和輸出。

示例

 即時演示

public class Demo {
   public static void main(String[] args) {
      String str = " a ";
      System.out.println("Text = "+str);
      System.out.println("Text after using trim() method = " + str.trim());
    }
}

輸出

Text = a
Text after using trim() method = a

更新於: 2020 年 6 月 26 日

417 次瀏覽

開啟您的 職業生涯

完成課程後獲得認證

開始
廣告