trim()
作用:去掉字符串中首尾的半角空格
使用:str.trim()
public static void main(String[] args) {
String str = " data ";
String trim = str.trim();//去掉首尾空格
System.out.println("原始字符串长度:"+str.length());
System.out.println(trim.length());
String replace = str.replace((char) 12288, ' ');
String trim1 = replace.trim();
System.out.println("去掉全角空格后字符串长度:"+trim1.length());
}
原始字符串长度:6
5
去掉全角空格后字符串长度:4
其中,12288 是中文全角空格的 unicode 编码。
全角输入为: