版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。
https://blog.csdn.net/weixin_43453386/article/details/83892962
1、左右空格都去
public static String trim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+|[ ]+$","");
}
}
2、去左空格
public static String leftTrim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+", "");
}
}
3、去右空格
public static String leftTrim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+", "");
}
}