字符串
1 字符串补位
String f = "%05d"; System.out.println(String.format(f, 100)); //在100的左边补0 , 总共有5位 String str1=String.format("%-7s", "100").replaceAll(" ", "0"); //在右边补0,可以将0换成其他的字符 System.out.println(str1); String str2=String.format("%7s", "100").replaceAll(" ", "0"); //在左边补0,可以将0换成其他的字符 System.out.println(str2);
2 字符串replace
String [] arr = new String[] {"abc","def","ert"}; System.out.println(Arrays.toString(arr)); //[abc, def, ert] System.out.println(Arrays.toString(arr).replaceAll("\\[|\\]", "").replaceAll(",\\s", ",")); //abc,def,ert
3 找出字符串中所有满足格式的子字符串
/** * 打印结果#{userAge} ,#{userName} * @param args */ public static void main(String[] args) { String sql = "update user set userAge = #{userAge} where userName = #{userName} "; Pattern pattern = Pattern.compile("#\\{.*?\\}"); Matcher matcher = pattern.matcher(sql); while (matcher.find()) { String e = matcher.group(0); System.out.println(e); } }
4 字符串格式化