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
廣告