1.用于JS去除空格
//Function trim a string
function trim(Str , Flag)
{
Str = ""+Str;
if( Flag == "l" || Flag == "L" )/*trim left side only*/
{
RegularExp = /^\s+/gi;
return Str.replace( RegularExp,"" );
}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
{
RegularExp = /\s+$/gi;
return Str.replace( RegularExp,"" );
}
else/*defautly, trim both left and right side*/
{
RegularExp = /^\s+|\s+$/gi;
return Str.replace( RegularExp,"" );
}
}
2.用于java去除空格
/**
* 去string中的空格、换行符、制表符等
* */
public static String replaceBlank(String str) {
String dest = "";
if (str!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}