Java String trim示例
时间:2020-02-23 14:35:15 来源:igfitidea点击:
字符串的TRIM方法用于删除前导和尾随空间。
有时,我们获得另外的领先或者尾随空间,程序无法按预期工作。
删除前导和尾随空格后,它会返回字符串对象。
String的trim方法不删除中间空格。
字符串修剪示例:
package org.igi.theitroad;
public class StringTrimExample {
public static void main(String[] args) {
String str = " Hello world from ";
//without trim()
System.out.println("Without trim");
System.out.println("----------------------");
System.out.println(str+"theitroad.com");
System.out.println("----------------------");
//with trim()
System.out.println("nWith trim");
System.out.println("----------------------");
System.out.println(str.trim()+"theitroad.com");
}
}
运行上面的程序时,我们将得到以下输出:
Without trim
---------------------
Hello world from theitroad.com
---------------------
With trim
---------------------
Hello world fromtheitroad.com

