问题描述:获取一个字符串,判断最后一位是否是汉字,并且判断最后一位是否是“元”字,如果不是汉字,则后面加“元”字,否则继续拼接。
Java用的是Unicode 编码char 型变量的范围是0-65535 无符号的值,可以表示 65536个字符。
unicode编码范围:
汉字:[0x4e00,0x9fa5](或十进制[19968,40869])
数字:[0x30,0x39](或十进制[48, 57])
小写字母:[0x61,0x7a](或十进制[97, 122])
大写字母:[0x41,0x5a](或十进制[65, 90])
汉字编码范围:\u4e00-\u9FA5
双字节字符编码范围:\u0391-\uFFE5
第一步:判断是否存在汉字
public boolean generateJudgment(String countname)
{
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher isNum = p.matcher(countname);
if (isNum.find()) {
return true;
}
return false;
}
第二步:结合第一步并判断最后一位是否包含“元”,否则继续拼接
public String generateJudgment() {
String str = "";
Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]"); //判断是否存在汉字
if (StringUtils.isNotEmpty(this.fine)) {
Matcher isNum = p.matcher(this.fine);
if (!isNum.find() && !this.fine.endsWith("元")) {
str += "奖金:" + this.fine + "元;";
} else {
str += "奖金:" + this.fine + ";";
}
}
if (StringUtils.isNotEmpty(this.pay)) {
Matcher isNum = p.matcher(this.pay);
if (!isNum.find() && this.pay.endsWith("元")) {
str += "罚金:" + this.pay+ "元;";
} else {
str += "罚金:" + this.pay+ ";";
}
}
return str;
}
解决,本次操作是项目开发中遇到的问题,在此记录。