package nyfz.util;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
/**
* 验证是否为数字
*
* @return
*/
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
char[] arr = str.toCharArray();
for (char c : arr) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
/**
* 验证某个字符是否为数字
*
* @param str
* @return
*/
public static boolean isNumber(String str) {
if (isEmpty(str)) {
return false;
}
// 该正则表达式可以匹配所有的数字 包括负数
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
String bigStr;
try {
bigStr = new BigDecimal(str).toString();
} catch (Exception e) {
return false;// 异常 说明包含非数字。
}
Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
if (!isNum.matches()) {
return false;
}
return true;
}
/**
* 替换空格、换行符等不可见字符
* @param str
* @return
*/
public static String replaceSpace(String str) {
String result = str;
if (str != null && str.length() > 0) { // 替换空格、换行符等不可见字符
String regEx = "\\s| {1,}";
Pattern pattern = Pattern.compile(regEx);
Matcher m = pattern.matcher(str);
resul