看到很多代碼都用了Trim()方法,string的Trim()方法是做什么的?有的資料上說是去掉空白字符,是不是把空格,制表符,回車等都去掉,能不能詳細一點?
2 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
Java String.trim()方法用法實例教程,該方法返回一個復制該字符串的開頭和結尾的白色空格去掉,或字符串,如果它沒有頭或尾空白.
package com.yiibai; import java.lang.*; public class StringDemo { public static void main(String[] args) { // string with leading and trailing white space String str = " This is YiiBai " ; System.out.print( "Before trim = " ); System.out.println( ".." + str + ".." ); // leading and trailing white space removed System.out.print( "After trim = " ); System.out.println( ".." + str.trim() + ".." ); } } |
添加回答
舉報
0/150
提交
取消