全角空格 数字表示 12288
半角空格 数字表示 32
一般string类中去掉空格的一般操作都是半角空格
String源码:public String trim()
{
int i = count;
int j = 0;
int k = offset;
char ac[];
for(ac = value; j < i && ac[k + j] <= ' '; j++);
for(; j < i && ac[(k + i) - 1] <= ' '; i--);
return j <= 0 && i >= count ? this : substring(j, i);
}
================================================================================
StringUtils源码:
public static String trimLeadingWhitespace(String str)
{
if(str.length() == 0)
return str;
StringBuffer buf;
for(buf = new StringBuffer(str); buf.length() > 0 && Character.isWhitespace(buf.charAt(0)); buf.deleteCharAt(0));
return buf.toString();
}
public static boolean isWhitespace(char c)
{
return isWhitespace(c); //此处调用的是底层的源码,经代码测试可以覆盖全角半角空格
}
================================================================================
下面是网络利用正则表达式计算的空格
/**
* 去除字符串中所包含的空格(包括:空格(全角,半角)、制表符、换页符等)
* @param s
* @return
*/
public static String removeAllBlank(String s){
String result = "";
if(null!=s && !"".equals(s)){
result = s.replaceAll("[ *| *|?*|//s*]*", "");
}
return result;
}
/**
* 去除字符串中头部和尾部所包含的空格(包括:空格(全角,半角)、制表符、换页符等)
* @param s
* @return
*/
public static String trim(String s){
String result = "";
if(null!=s && !"".equals(s)){
result = s.replaceAll("^[ *| *|?*|//s*]*", "").replaceAll("[ *| *|?*|//s*]*$", "");
}
return result;
}
}
来源:oschina
链接:https://my.oschina.net/feixuewuhen/blog/4302402