1. 参考:2. 常用:
1)替换连续的空
//将字符串中连续的空格去掉
String str = "Hello, world. haha !";
System.out.println(str.replaceAll(" +", " "));
System.out.println(str.replaceAll(" {2,}", " "));
System.out.println(str.replaceAll("\\s{2,}", " "));
Hello, world. haha !
Hello, world. haha !
Hello, world. haha !
String strDouble = "10900.000";
// String strRes = strDouble.replaceAll("0+$", "");
String strRes = strDouble.replaceAll("0+?$", "");
// strRes = strRes.replaceAll(".$", "");
strRes = strRes.replaceAll("[.]$", "");
System.out.println(strRes);
output:
10900
3) ip地址合格检验
参考:http://mxdxm.iteye.com/blog/2087042
public static boolean isIpv4(String ipAddress) {
String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}