/** * 字符串去两边空格 * @param string * @return */ public static String removeEmptyCharLeftAndRight(String string) { string = removeEmptyCharLeft(string); string = removeEmptyCharright(string); return string; } /** * 字符串去左边空格 * @param string * @return */ public static String removeEmptyCharLeft(String string) { if(StringUtils.isBlank(string)) { return ""; } char[] cs = string.toCharArray(); boolean empty = false; for(char c :cs) { empty = Pattern.matches("[ |\\s]", String.valueOf(c)); if(empty) { string = string.substring(1); }else{ break; } } return string; } /** * 字符串去右边空格 * @param string * @return */ public static String removeEmptyCharright(String string) { if(StringUtils.isBlank(string)) { return ""; } char[] cs = string.toCharArray(); boolean empty = false; for(int i=cs.length;i>0;i--) { empty = Pattern.matches("[ |\\s]", String.valueOf(cs[i-1])); if(empty) { string = string.substring(0,string.length()-1); }else{ break; } } return string; }