📄 stringutils.java
字号:
text = text.replaceAll("\"", """);
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
text = text.replaceAll("'", "’");
text = text.replaceAll(" ", " ");
text = text.replaceAll("\n", "<br>");
text = text.replaceAll("\t", " ");
return text;
}
/**
* 把输入的字符串中特定符号全部替换为对应的符号
*
* @param str
* String 要操作的字符串
* @param flag
* String[] 要匹配的符号
* @param value
* String[] 每个匹配符号对应的替换值
* @return String 操作后的字符串
*/
public static String formatString(String str, String flag[], String value[]) {
Pattern p = null;
Matcher m = null;
for (int i = 0; i < flag.length; i++) {
p = Pattern.compile(flag[i]);
m = p.matcher(str);
str = m.replaceAll(value[i]);
}
return str;
}
/**
* 格式化输出货币字符串,如 2.23
*
* @param money
* String
* @return String
*/
public static String formatMoney(String money) {
String res = "";
try {
DecimalFormat df = new DecimalFormat("#.00");
res = df.format(Double.parseDouble(money));
if (Double.parseDouble(res) <= 0) {
res = "";
}
} catch (Exception ex) {
}
return res;
}
/**
* 四舍五入
*
* @param money
* String
* @return String
*/
public static String formatRound(String num, int len) {
String res = "";
String slen = "";
for (int i = 0; i < len; i++) {
slen = slen + "0";
}
try {
DecimalFormat df = new DecimalFormat("#." + slen);
res = df.format(Double.parseDouble(num));
if (Double.parseDouble(res) <= 0) {
res = "";
}
} catch (Exception ex) {
}
return res;
}
/**
* head 表示格式化后数据的前缀,sepNum表示货币分割符号的位数 调用 formatMoney("12000000","¥" ,3) 输出
* ¥12,000,000
*
* @param money
* String
* @param head
* String
* @param sepNum
* int
* @return String
*/
public static String formatMoney(String money, String head, int sepNum) {
String res = "";
try {
DecimalFormat df = new DecimalFormat("#.00");
res = df.format(Double.parseDouble(money));
if (Double.parseDouble(res) <= 0) {
res = head + "0.00";
} else {
String s = head;
String endstr = res.substring(res.length() - sepNum, res
.length());
String tempstr = res.substring(0, res.length() - sepNum);
String startstr = "";
while (sepNum < tempstr.length()) {
startstr = ","
+ tempstr.substring(tempstr.length() - sepNum,
tempstr.length()) + startstr;
tempstr = tempstr.substring(0, tempstr.length() - sepNum);
}
res = s + tempstr + startstr + endstr;
}
} catch (Exception ex) {
}
return res;
}
public static boolean checkContain(String[] array, String key) {
if (null == array) {
return false;
}
for (int i = 0; i < array.length;) {
if (array[i++].equals(key)) {
return true;
}
}
return false;
}
/**
* 取得不包含国际号的电话号码
*
* @param tel
* String
* @return String
*/
public static String getShortTelephone(String tel) {
String res = "";
if (tel.startsWith("(")) {
int k = tel.indexOf(")");
if (k > 0) {
res = tel.substring(k + 1);
}
} else {
res = tel;
}
return res;
}
/**
* 检查字符串是否为正确的email地址
*
* @param email
* String
* @return boolean
*/
public static boolean isEmailAddress(String email) {
Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");
Matcher m = p.matcher(email);
return m.matches();
}
/**
* 统计字符串中某一个字符出现的次数
*
* @param source
* String
* @param word
* char
* @return int
*/
public static int count(String source, char word) {
int len = source.length();
int cnt = 0;
for (int i = 0; i < len; i++) {
if (source.charAt(i) == word) {
cnt++;
}
}
return cnt;
}
// 简单的随机码生成
// int nRandomCount 长度
// boolean isNumber 是否是数字代码
// By longware
public static String getRandomString(int nRandomCount, boolean isNumber) {
char[] a = null;
if (isNumber) {
a = new char[10];
for (int i = 48, j = 0; i <= 57; i++, j++) {
a[j] = (char) i;
}
} else {
// ASCII 48~57,String 0~9;
// ASCII 65~90,String A-Z;
// ASCII 97~122,String a-z;
a = new char[62];
for (int i = 48, j = 0; i <= 122; i++) {
if ((i > 57 && i < 65) || (i > 90 && i < 97)) {
continue;
} else {
a[j] = (char) i;
j++;
}
}
}
String strRand = "";
int LengthOfRandom = a.length;
Random random = new Random();
for (int i = 0; i < nRandomCount; i++) {
int nRand = random.nextInt(LengthOfRandom);
strRand += a[nRand];
}
return strRand;
}
/**
* 格式化高级查询参数 如:input "1,2,3" output "*1*,*2*,*3*"
*
* @param key
* String
* @return String
*/
public static String formatHighSearchKey(String key, String sep) {
String result = "";
if (!key.equals("")) {
if (key.length() > 3) {
if (key.substring(0, 1).equals("[")) {
key = key.substring(1, key.length() - 1);
}
String[] ids = key.split(", ");
for (int idx = 0; idx < ids.length; idx++) {
if (result.equals("")) {
result = sep + ids[idx] + sep;
} else {
result = result + "," + sep + ids[idx] + sep;
}
}
} else {
result = sep + key + sep;
}
}
return result;
}
/**
* 根据内容模板和参数生成字符串结果,简易方式,直接用字符串进行替换
*
* @param pathBase
* String
* @param templateName
* String
* @param tags
* Hashtable
* @return String
* @throws Exception
*/
public static String genStringFromContentTemplate(String contentTemplate,
Map tags) throws Exception {
String content = contentTemplate;
Iterator i = tags.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
String value = String.valueOf(tags.get(key));
if (value != null) {
key = "\\$" + key;
content = content.replaceAll(key, value);
}
}
return content;
}
/**
* 根据内容模板和参数生成字符串结果,简易方式,直接用字符串进行替换 {参数}
*
* @param pathBase
* String
* @param templateName
* String
* @param tags
* Hashtable
* @return String
* @throws Exception
*/
public static String genStringFromContentTemplate2(String contentTemplate,
Map tags) throws Exception {
String content = contentTemplate;
Iterator i = tags.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
String value = String.valueOf(tags.get(key));
if (value != null) {
key = "\\{" + key + "\\}";
content = content.replaceAll(key, value);
}
}
return content;
}
public static String formatValues(String src[]) {
String res = "";
try {
if (src != null && src.length > 0) {
for (int i = 0; src.length > i; i++) {
if (res.equals("")) {
res = "'" + src[i] + "'";
} else {
res = res + "," + "'" + src[i] + "'";
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return res;
}
/**
* 转换小写数字金额为大写
*
* @author zbh 2006-03-29
* @param s
* String
* @return String
*/
public static String formatMoneyToChinses(String s) {
if (s == null || s.equals("")) {
return "";
}
String money = "";// 转换后的字符串
String num = "零壹贰叁肆伍陆柒捌玖";
String[] unit = { "元", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万", "亿", "拾亿",
"佰亿", "仟亿" };
int a = s.indexOf("+");// 判断s是否包含'+',如1.67E+4
int e = s.indexOf("E");// 判断s是否包含'E',如1.67E+4
// 如果包含'E'(该金额是以科学记数法表示,则转换成普通表示法)
if (e != -1) {
int index = 0;// 指数值
if (a == -1)
index = Integer.parseInt(s.substring(e + 1));// 取得指数值
else
index = Integer.parseInt(s.substring(a + 1));// 取得指数值
String sub1 = s.substring(0, e);// 取得尾数值
int dot = sub1.indexOf(".");// 尾数的小数点位置
// 如果不含有小数点,则在后面补index个'0'
if (dot == -1) {
for (int i = 1; i <= index; i++) {
s = sub1 + "0";
}
} else {// 如果含有小数点,则向后移动小数点index位
String sub11 = sub1.substring(0, dot);// 小数点前面的字串
String sub12 = sub1.substring(dot + 1);// 小数点后面的字串
if (index >= sub12.length()) {
int j = index - sub12.length();
for (int i = 1; i <= j; i++) {
sub12 = sub12 + "0";
}
} else {
sub12 = sub12.substring(0, index) + "."
+ sub12.substring(index);
}
s = sub11 + sub12;
}
}
int sdot = s.indexOf(".");// s中小数点的位置
String beforeDot = "";// 小数点前面的字串
String afterDot = "";// 小数点后面的字串
// 如果包含小数点
if (sdot != -1) {
beforeDot = s.substring(0, sdot);
afterDot = s.substring(sdot + 1);
} else {// 不包含小数点
beforeDot = s;
}
int bl = beforeDot.length();
boolean zero = false;// 数字是否为零
int z = 0;// '0'的个数
// 逐位取数字
for (int j = 0, i = bl - 1; j <= bl - 1; j++, i--) {
int number = Integer.parseInt(String.valueOf(beforeDot.charAt(j)));
if (number == 0) {
zero = true;
z++;
} else {
zero = false;
z = 0;
}
if (zero && z == 1) {
money += "零";
} else if (zero) {
} else if (!zero) {
money += num.substring(number, number + 1) + unit[i];
}
}
// 删去多余的'万'和'亿'
for (int i = 1; i <= 2; i++) {
String ss = "";
if (i == 1)
ss = "万";
else
ss = "亿";
int last = money.lastIndexOf(ss);
if (last != -1) {
String moneySub1 = money.substring(0, last);
String moneySub2 = money.substring(last, money.length());
int last2 = moneySub1.indexOf(ss);
while (last2 != -1) {
moneySub1 = moneySub1.substring(0, last2)
+ moneySub1
.substring(last2 + 1, moneySub1.length());
last2 = moneySub1.indexOf(ss);
}
money = moneySub1 + moneySub2;
}
}
// money中是否包含'元'
int yuan = money.indexOf("元");
// 如果不包含'元'
if (yuan == -1) {
int zi = money.lastIndexOf("零");
// 如果最后一位字符为'零',则删除它
if (zi == money.length() - 1) {
money = money.substring(0, money.length() - 1) + "元";// 在money最后加上'元'
}
}
// 如果小数点后面的字串不为空,则处理'角','分'
if (!afterDot.equals("")) {
int al = afterDot.length();
if (al > 2) {// 如果字串长度大于2,则截断
afterDot = afterDot.substring(0, 2);
al = afterDot.length();
}
// 如果字符串不为'0'或'00',则处理,否则不进行处理
if (!afterDot.equals("0") && !afterDot.equals("00")) {
// 逐位取得字符
for (int i = 0; i < al; i++) {
int number = Integer.parseInt(String.valueOf(afterDot
.charAt(i)));
if (number != 0 && i == 0) {
money += num.substring(number, number + 1) + "角";
} else if (number != 0 && i == 1) {
money += num.substring(number, number + 1) + "分";
} else if (number == 0 && i == 0) {
money += "零";
}
}
}
}
// 如果不包含'角','分'则在最后加上'整'字
if (money.indexOf("角") == -1 && money.indexOf("分") == -1)
money += "整";
System.out.println(s);
System.out.println(money);
return money;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -