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 次浏览

开启您的 职业生涯

完成课程后获得认证

开始
广告
© . All rights reserved.