Files
mosty-dyga-cloud/mosty-hczx/src/main/java/com/mosty/hczx/utils/StringUtils.java
esacpe b80c560e87 1
2024-07-17 21:00:42 +08:00

1106 lines
30 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.mosty.hczx.utils;
import com.mosty.base.utils.DateUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* 字符串处理类<br>
*
* @author redstorm
* @version 2011-3-15
*
*/
public class StringUtils {
private static Logger logger = LogManager.getLogger(StringUtils.class);
public static final String enterNewline = String.valueOf((char) 13)
+ String.valueOf((char) 10); // windows下的换行符
public static final String enterNewline_linux = String.valueOf((char) 10); // linux下的换行符
public StringUtils() {
}
/**
* null 转换为空字符串<br>
*
* @param temp
* 需要转换的字符串
* @return String
*/
public static String nullToStr(String temp) {
if (temp == null)
temp = "";
return temp;
}
/**
* 判断字符串是否为空空格、回车符、换行符、Tab符默认为空<br>
*
* @param str
* 判断的字符串
* @return boolean 返回是否为空(为null时返回true)
*/
public static boolean isBlank(String temp) {
boolean returnValue = true;
if (temp != null) {
returnValue = temp.matches("\\s*");
}
return returnValue;
}
/**
* 两个字符串连接null 按空字符串处理<br>
*
* @param str1
* 字符串1
* @param str2
* 字符串2
* @return String
*/
public static String stringConcat(String str1, String str2) {
String temp = "";
if (str1 != null) {
temp = str1;
}
if (str2 != null) {
temp = temp.concat(str2);
}
return temp;
}
/**
* 返回字符串分割后的字符串数组,不同于 String.split() 方法,可以正确的得到特殊字符的分割结果<br>
*
* @param str
* 被分割的字符串
* @param splitStr
* 分割符,可以是一个字符串
* @return String[]
*/
public static String[] split(String str, String splitStr) {
List<String> coll = new ArrayList<String>();
if (str != null && !"".equals(str) && splitStr != null
&& !"".equals(splitStr)) {
String tempStr = str;
int at = tempStr.indexOf(splitStr);
while (at != -1) {
coll.add(tempStr.substring(0, at));
tempStr = tempStr.substring(at + splitStr.length());
at = tempStr.indexOf(splitStr);
}
coll.add(tempStr);
}
return (String[]) coll.toArray(new String[0]);
}
/**
* 去掉字符串的单引号与双引号<br>
*
* @param str
* @return String
*/
public static String clearMark(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("'", "");
returnValue = returnValue.replaceAll("\"", "");
}
return returnValue;
}
/**
* 文本字符串转换成HTML格式<br>
*
* @param str
* 需要转换的字符串 <li>& 转换成 amp; <li>空格 转换成 nbsp; <li>" 转换成 quot;
* <li>< 转换成 lt; <li>>转换成 gt; <li>\n 转换成 br
* @return String
*/
public static String txtToHtml(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("&", "&amp;");
returnValue = returnValue.replaceAll(" ", "&nbsp;");
returnValue = returnValue.replaceAll("\"", "&quot;");
returnValue = returnValue.replaceAll("<", "&lt;");
returnValue = returnValue.replaceAll(">", "&gt;");
returnValue = returnValue.replaceAll("\n", "<br>");
}
return returnValue;
}
/**
* 文本字符串转换成HTML格式<br>
* (不替换 &符号)
*
* @param str
* 需要转换的字符串 <li>空格 转换成 nbsp; <li>" 转换成 quot; <li>< 转换成 lt; <li>>
* 转换成 gt; <li>\n 转换成 br
* @return String
*/
public static String txtToHtml2(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll(" ", "&nbsp;");
returnValue = returnValue.replaceAll("\"", "&quot;");
returnValue = returnValue.replaceAll("<", "&lt;");
returnValue = returnValue.replaceAll(">", "&gt;");
returnValue = returnValue.replaceAll("\n", "<br>");
}
return returnValue;
}
/**
* HTML格式的字符串转换成文本格式<br>
*
* @param str
* 需要转换的字符串 <li>amp; 转换成 & <li>nbsp; 转换成 空格 <li>quot; 转换成 "
* <li>lt; 转换成 < <li>gt; 转换成 > <li>br 转换成 \n <li>br/ 转换成 \n
* @return String
*/
public static String htmlToTxt(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = returnValue.replaceAll("<br/>", "\n");
returnValue = returnValue.replaceAll("<br>", "\n");
returnValue = returnValue.replaceAll("&lt;", "<");
returnValue = returnValue.replaceAll("&gt;", ">");
returnValue = returnValue.replaceAll("&nbsp;", " ");
returnValue = returnValue.replaceAll("&amp;", "&");
returnValue = returnValue.replaceAll("&quot;", "\"");
}
return returnValue;
}
/**
* 当前字符转换行符转换成空格<br>
*
* @param str
* 需要转换的字符串 (char)13 + (char)10 转换成 空格
* @return String
*/
public static String enterToSpace(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("\n", " ");
returnValue = returnValue.replaceAll(enterNewline, " ");
returnValue = returnValue
.replaceAll(String.valueOf((char) 13), " ");
}
return returnValue;
}
/**
* 当前字符转换行符转换成空格<br>
*
* @param str
* 需要转换的字符串 (char)13 + (char)10 转换成 空字符串
* @return String
*/
public static String enterToEmpty(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("\n", "");
returnValue = returnValue.replaceAll(enterNewline, "");
returnValue = returnValue.replaceAll(String.valueOf((char) 13), "");
}
return returnValue;
}
/**
* 取得SQL中的条件表达式<br>
*
* @param str
* 需要转换的字符串 '(单引号) 转换成 ''两个单引号SQL中默认两个单引号为一个单引号字符
* @return String
*/
public static String getSqlExpression(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("'", "''");
}
return returnValue;
}
/**
* 当前字符转换行符转换成回车符<br>
*
* @param str
* 需要转换的字符串 (char)13 + (char)10 转换成 (char)13 回车符
* @return String
*/
public static String enterConvert(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll(enterNewline, String
.valueOf((char) 13));
}
return returnValue;
}
/**
* 取得JS的字符串主要处理特殊字符<br>
*
* @param str
* @return String
*/
public static String getStringForJS(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str;
returnValue = returnValue.replaceAll("\\\\", "\\\\\\\\");
returnValue = returnValue.replaceAll("\"", "\\\\\"");
returnValue = returnValue.replaceAll(enterNewline, "\\n");
returnValue = returnValue.replaceAll(String.valueOf((char) 13),
"\\n");
returnValue = returnValue.replaceAll("\n", "\\n");
}
return returnValue;
}
/**
* 取得JS要替换的字符串正则表达式主要处理特殊字符<br>
*
* @param str
* @return String
*/
public static String getStringForJSReplace(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str;
returnValue = returnValue.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");
returnValue = returnValue.replaceAll("\"", "\\\\\\\\\"");
returnValue = returnValue.replaceAll(enterNewline, "\\\\\\\\n");
returnValue = returnValue.replaceAll(String.valueOf((char) 13),
"\\\\\\\\n");
returnValue = returnValue.replaceAll("\n", "\\\\\\\\n");
}
return returnValue;
}
/**
* 取得当前URL完整地址包括参数字符串<br>
*
* @param request
* 当前的 request 对象
* @return String
*/
public static String getCurURL(HttpServletRequest request) {
String returnValue = request.getRequestURI();
String queryString = request.getQueryString();
if (queryString != null) {
returnValue = returnValue + "?" + queryString;
}
return returnValue;
}
/**
* 取得当前URL替换后的完整地址<br>
* <br>
* 注意如替换的参数名不存则增加参数值为中文直接用URLEncode方法转换queryString中如有多个相同的参数则同时替换
*
* @param request
* 当前的 request 对象
* @param parameterMap
* 替换的参数名与值的Map集合
* @return String
*/
@SuppressWarnings("rawtypes")
public static String getCurURLReplaced(HttpServletRequest request,
Map parameterMap) {
String requestUri = request.getRequestURI();
String queryString = request.getQueryString();
return getCurURLReplaced(requestUri, queryString, parameterMap);
}
/**
* 取得当前URL替换后的完整地址<br>
* <br>
* 注意如替换的参数名不存则增加参数值为中文直接用URLEncode方法转换queryString中如有多个相同的参数则同时替换
*
* @param requestUri
* 当前的请求地址
* @param queryString
* 当前请求地址中的get参数字符串
* @param parameterMap
* 替换的参数名与值的Map集合
* @return String
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String getCurURLReplaced(String requestUri,
String queryString, Map parameterMap) {
String returnValue = "";
requestUri = nullToStr(requestUri);
if (parameterMap == null || parameterMap.isEmpty()) { // 参数为空
if (queryString == null || "".equals(queryString)) {
returnValue = requestUri;
} else {
returnValue = requestUri + "?" + queryString;
}
} else {
String key, value;
if (queryString == null || "".equals(queryString)) { // 本身没有参数
StringBuffer sb = new StringBuffer();
Iterator iterator = parameterMap.keySet().iterator();
while (iterator.hasNext()) { // 不存在的新增
key = (String) iterator.next();
value = (String) parameterMap.get(key);
if (sb.length() == 0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(key + "=" + URLEncode(value));
}
returnValue = requestUri + sb.toString();
} else { // 本身有参数
StringBuffer sb = new StringBuffer();
String[] tempArray = queryString.split("&");
Map addMap = new HashMap(parameterMap);
for (int i = 0; i < tempArray.length; i++) {
String[] keyArray = tempArray[i].split("=");
key = keyArray[0];
sb.append("&");
if (parameterMap.containsKey(key)) {
value = (String) parameterMap.get(key);
sb.append(key + "=" + URLEncode(value));
addMap.remove(key);
} else {
sb.append(tempArray[i]);
}
}
Iterator iterator = addMap.keySet().iterator();
while (iterator.hasNext()) { // 不存在的新增
key = (String) iterator.next();
value = (String) addMap.get(key);
sb.append("&" + key + "=" + URLEncode(value));
}
returnValue = requestUri + "?" + sb.toString().substring(1);
}
}
return returnValue;
}
/**
* 中文字符串转换,转成 ISO8859-1 编码格式<br>
*
* @param str
* @return String
*/
public static String getCharSetISO8859(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray);
return temp;
} catch (Exception ex) {
logger.error("中文字符串转换出错ISO8859: " + ex.getMessage(), ex);
}
return "";
}
/**
* 中文字符串转换GBK转成 UTF8 编码格式<br>
*
* @param str
* @return String
*/
public static String getGBK_UTF8(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("GBK");
String temp = new String(tempArray, "UTF-8");
return temp;
} catch (Exception ex) {
logger.error("中文字符串转换出错GBK_UTF8: " + ex.getMessage(), ex);
}
return "";
}
/**
* 中文字符串转换UTF8转成GBK 编码格式<br>
*
* @param str
* @return String
*/
public static String getUTF8_GBK(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("UTF-8");
String temp = new String(tempArray, "GBK");
return temp;
} catch (Exception ex) {
logger.error("中文字符串转换出错UTF8_GBK: " + ex.getMessage(), ex);
}
return "";
}
/**
* URL中的中文字符串转换<br>
*
* <p>
* 说明没有通过页面提交的则用java.net.URLDecoder.decode(str, "UTF-8")还原
* </p>
*
* @param str
* @return String
*/
public static String URLEncode(String str) {
String returnValue = "";
try {
if (str != null) {
returnValue = java.net.URLEncoder.encode(str, "UTF-8");
}
} catch (Exception ex) {
logger.error("URL中的中文字符串转换出错: " + ex.getMessage(), ex);
}
return returnValue;
}
/**
* URL中的中文字符串还原<br>
*
* <p>
* 说明直接用URLEncode()的返回值调用该方法是无效的,必须是在请求页面中调用才能还原,因为页面请求是按 ISO8859-1 格式编码的
* </p>
*
* @param str
* @return String
*/
public static String URLDecode(String str) {
String returnValue = "";
try {
if (str != null) {
byte[] tempArray = str.getBytes("ISO8859-1");
returnValue = new String(tempArray, "UTF-8");
}
} catch (Exception ex) {
logger.error("URL中的中文字符串还原出错: " + ex.getMessage(), ex);
}
return returnValue;
}
/**
* 取得字符串长度(一个汉字为两个字符长度)<br>
*
* @param sourceStr
* 需要处理的字符串
* @return long
*/
public static long getStringLength(String sourceStr) {
long returnValue = 0;
if (sourceStr == null) {
return (returnValue);
}
for (int i = 0; i < sourceStr.length(); i++) {
char[] tempChar = sourceStr.substring(i, i + 1).toCharArray();
if ((int) tempChar[0] > 255)
returnValue += 2;
else
returnValue++;
}
return returnValue;
}
/**
* 取得重复次数的字符串<br>
*
* @param sourceStr
* 重复源
* @param repeatTimes
* 重复次数
* @return String
*/
public static String getRepeatString(String sourceStr, long repeatTimes) {
if (repeatTimes == 0) {
return "";
}
StringBuffer returnStr = new StringBuffer();
for (int i = 0; i < repeatTimes; i++) {
returnStr.append(sourceStr);
}
return returnStr.toString();
}
/**
* 取得指定长度的左字符串,不足长度的以 replaceString 右填充<br>
*
* @param sourceStr
* 需要处理的字符串
* @param specityLength
* 指定长度(一个汉字为两个字符长度,如果截断地方为一个汉字,则可能长度小1)
* @param replaceString
* 不足长度时填充的字符
* @return String
*/
public static String getLeftSpecifyString(String sourceStr,
long specityLength, String replaceString) {
if (sourceStr == null) {
return (getRepeatString(replaceString, specityLength));
}
long realLength = getStringLength(sourceStr);
StringBuffer returnStr = new StringBuffer();
if (realLength < specityLength) {
returnStr.append(sourceStr);
returnStr.append(getRepeatString(replaceString, specityLength
- realLength));
} else {
returnStr.append(getLeftString(sourceStr, specityLength));
}
return returnStr.toString();
}
/**
* 取得字符串的左子串,长度由 leftLength 指定<br>
*
* @param sourceStr
* 需要处理的字符串
* @param leftLength
* 指定长度,从左边取该长度(一个汉字为两个字符长度,如果截断地方为一个汉字,则可能长度小1)
* @return String
*/
public static String getLeftString(String sourceStr, long leftLength) {
StringBuffer returnStr = new StringBuffer();
long tempLength = 0;
for (int i = 0; i < sourceStr.length(); i++) {
String tempStr = sourceStr.substring(i, i + 1);
char[] tempChar = tempStr.toCharArray();
if ((int) tempChar[0] > 255)
tempLength += 2;
else
tempLength++;
if (tempLength < leftLength) {
returnStr.append(tempStr);
} else {
if (tempLength == leftLength)
returnStr.append(tempStr);
break;
}
}
return returnStr.toString();
}
/**
* 取得指定长度的右字符串,不足长度的以 replaceString 左填充<br>
*
* @param sourceStr
* 需要处理的字符串
* @param specityLength
* 指定长度(一个汉字为两个字符长度,如果截断地方为一个汉字,则可能长度小1)
* @param replaceString
* 不足长度时填充的字符
* @return String
*/
public static String getRightSpecifyString(String sourceStr,
long specityLength, String replaceString) {
if (sourceStr == null) {
return (getRepeatString(replaceString, specityLength));
}
long realLength = getStringLength(sourceStr);
StringBuffer returnStr = new StringBuffer();
if (realLength < specityLength) {
returnStr.append(getRepeatString(replaceString, specityLength
- realLength));
returnStr.append(sourceStr);
} else {
returnStr.append(getRightString(sourceStr, specityLength));
}
return returnStr.toString();
}
/**
* 取得字符串的右子串,长度由 leftLength 指定<br>
*
* @param sourceStr
* 需要处理的字符串
* @param rightLength
* 指定长度,从右边取该长度(一个汉字为两个字符长度,如果截断地方为一个汉字,则可能长度小1)
* @return String
*/
public static String getRightString(String sourceStr, long rightLength) {
StringBuffer returnStr = new StringBuffer();
long tempLength = 0;
for (int i = sourceStr.length() - 1; i >= 0; i--) {
String tempStr = sourceStr.substring(i, i + 1);
char[] tempChar = tempStr.toCharArray();
if ((int) tempChar[0] > 255)
tempLength += 2;
else
tempLength++;
if (tempLength < rightLength) {
returnStr.insert(0, tempStr);
} else {
if (tempLength == rightLength)
returnStr.insert(0, tempStr);
break;
}
}
return returnStr.toString();
}
/**
* 取得指定长度的字符串,长度由 targetlength 指定<br>
*
* @param sourceString
* 需要处理的字符串
* @param targetlength
* 指定长度,从左边取该长度(一个汉字为两个字符长度)
* @return String
*/
public static String displayTitle(String sourceString, long targetlength) {
String returnValue = "";
if (sourceString != null) {
if (getStringLength(sourceString) <= targetlength * 2)
returnValue = sourceString;
else
returnValue = getLeftString(sourceString,
(targetlength - 1) * 2)
+ "";
}
return returnValue;
}
/**
* 把格式化的日期转换为没有分隔符的字数日期
*
* @param s
* @return
*/
public static String changeTime(String s) {
String time = "";
if (!"".equals(s)) {
String[] ss = s.split(" ");
if (ss.length == 2) {
String[] sss = ss[0].split("-");
String[] sss2 = ss[1].split(":");
for (int i = 0; i < sss.length; i++) {
time = time + sss[i];
}
for (int i = 0; i < sss2.length; i++) {
time = time + sss2[i];
}
} else if (ss.length == 1) {
String[] sss = ss[0].split("-");
if (sss.length == 1) {
time = sss[0];
} else {
for (int i = 0; i < sss.length; i++) {
time = time + sss[i];
}
}
}
}
return time;
}
/**
* 取得人民币大写金额<br>
*
* @param money
* @return String
*/
public static String getChineseMoney(double money) {
if (money > 999999999999999.99 || money < 0) { // 不符合的数值
return "";
}
if (money == 0) {
return "零元整";
}
StringBuffer returnValue = new StringBuffer();
String chinaDigital[] = new String[] { "", "", "", "", "", "",
"", "", "", "" };
String chinaUnit[] = new String[] { "", "", "", "", "", "", "",
"亿", "", "", "", "", "", "", "", "", "", "", "" };
java.text.DecimalFormat fmt = new java.text.DecimalFormat(".00");
String moneyStr = fmt.format(money);
int moneyLength = moneyStr.length();
String tempChar = "";
int tempInt = 0;
int tempAtArray = 0;
int zeroCount = 0; // 记0个数
boolean lastZero = false;
for (int i = 0; i < moneyLength; i++) {
tempChar = moneyStr.substring(i, i + 1);
if (".".equals(tempChar)) {
if (money >= 1) {
returnValue.append("");
}
} else {
tempInt = Integer.parseInt(tempChar);
tempAtArray = 19 - moneyLength + i;
if (tempInt == 0) { // 为零的处理
lastZero = true;
zeroCount++;
if (tempAtArray == 7
|| (tempAtArray % 4 == 3 && zeroCount < 4)) { // 亿必需出现
returnValue.append(chinaUnit[tempAtArray]);
zeroCount = 0;
}
} else {
zeroCount = 0;
if (lastZero) {
if (money >= 1) {
returnValue.append(chinaDigital[0]);
}
}
returnValue.append(chinaDigital[tempInt]);
returnValue.append(chinaUnit[tempAtArray]);
lastZero = false;
}
}
}
if (moneyStr.endsWith("0")) {
returnValue.append("");
}
return returnValue.toString();
}
/**
* 判断字符串是否为空空格、回车符、换行符、Tab符默认为空<br>
*
* @param str
* 判断的字符串
* @return boolean 返回是否为空(为null时返回true)
*/
public static boolean isBlank(Object temp) {
boolean returnValue = true;
if (temp != null) {
if (temp instanceof String)
returnValue = temp.toString().matches("\\s*");
else
returnValue = false;
}
return returnValue;
}
/**
* 取得用人民币大写金额表示的整数,格式如: (贰仟零捌万零伍佰壹拾贰)<br>
*
* @param money
* @return String
*/
public static String getChineseNumberAsMoney(long money) {
if (money > 999999999999999.99 || money < 0) { // 不符合的数值
return "";
}
if (money == 0) {
return "";
}
StringBuffer returnValue = new StringBuffer();
String chinaDigital[] = new String[] { "", "", "", "", "", "",
"", "", "", "" };
String chinaUnit[] = new String[] { "", "", "", "", "", "", "",
"亿", "", "", "", "", "", "", "", "", "", "", "" };
java.text.DecimalFormat fmt = new java.text.DecimalFormat(".00");
String moneyStr = fmt.format(money);
int moneyLength = moneyStr.length();
String tempChar = "";
int tempInt = 0;
int tempAtArray = 0;
int zeroCount = 0; // 记0个数
boolean lastZero = false;
for (int i = 0; i < moneyLength; i++) {
tempChar = moneyStr.substring(i, i + 1);
if (".".equals(tempChar)) {
if (money >= 1) {
// returnValue.append("元");
}
} else {
tempInt = Integer.parseInt(tempChar);
tempAtArray = 19 - moneyLength + i;
if (tempInt == 0) { // 为零的处理
lastZero = true;
zeroCount++;
if (tempAtArray == 7
|| (tempAtArray % 4 == 3 && zeroCount < 4)) { // 亿必需出现
returnValue.append(chinaUnit[tempAtArray]);
zeroCount = 0;
}
} else {
zeroCount = 0;
if (lastZero) {
if (money >= 1) {
returnValue.append(chinaDigital[0]);
}
}
returnValue.append(chinaDigital[tempInt]);
returnValue.append(chinaUnit[tempAtArray]);
lastZero = false;
}
}
}
return returnValue.toString();
}
/**
* 取得 0 到 999999999 之间的数字中文字符串,格式如: (二千零八万零五百一十二)<br>
*
* @param num
* @return String
*/
public static String getChineseNumber(long number) {
if (number > 999999999 || number < 0) { // 不符合的数值
return "";
}
if (number == 0) {
return "";
}
StringBuffer returnValue = new StringBuffer();
String chinaDigital[] = new String[] { "", "", "", "", "", "",
"", "", "", "" };
String chinaUnit[] = new String[] { "亿", "", "", "", "", "", "",
"", "" };
String tempStr = "" + number;
int numberLength = tempStr.length();
String tempChar = "";
int tempInt = 0;
int tempAtArray = 0;
int zeroCount = 0;
boolean lastZero = false;
for (int i = 0; i < numberLength; i++) {
tempChar = tempStr.substring(i, i + 1);
tempInt = Integer.parseInt(tempChar);
tempAtArray = 9 - numberLength + i;
if (tempInt == 0) { // 为零的处理
lastZero = true;
zeroCount++;
if (tempAtArray == 0 || (tempAtArray % 4 == 0 && zeroCount < 4)) { // 亿必需出现
returnValue.append(chinaUnit[tempAtArray]);
zeroCount = 0;
}
} else {
zeroCount = 0;
if (lastZero) {
if (number >= 1) {
returnValue.append(chinaDigital[0]);
}
}
returnValue.append(chinaDigital[tempInt]);
returnValue.append(chinaUnit[tempAtArray]);
lastZero = false;
}
}
return returnValue.toString();
}
/**
* 将 15 位身份证号码转换为 18 位身份证号码<br>
*
* @param sfzh
* @return String
*/
public static String convertTo18Sfzh(String sfzh) {
String returnValue = sfzh;
try {
if (sfzh.length() == 15) {
String tempStr1 = sfzh.substring(0, 6);
String tempStr2 = "19" + sfzh.substring(6);
String tempStrAll = tempStr1 + tempStr2;
int lastAt = 0;
for (int i = 0; i < 17; i++) {
int bitInt = Integer.parseInt(tempStrAll
.substring(i, i + 1));
int bitIntTemp = 1;
for (int j = 0; j < 17 - i; j++) {
bitIntTemp = (bitIntTemp * 2) % 11;
}
lastAt += bitInt * bitIntTemp;
}
lastAt = lastAt % 11;
returnValue = tempStrAll
+ "10X98765432".substring(lastAt, lastAt + 1);
}
} catch (Exception ex) {
logger.error("转换成18位身份证号码出错: " + ex.getMessage(), ex);
}
return returnValue;
}
/**
* 是否为一个有效的 18 位身份证号码<br>
*
* @param sfzh
* @return boolean
*/
@SuppressWarnings("unused")
public static boolean isValidSfzh(String sfzh) {
boolean returnValue = false;
try {
if (!isBlank(sfzh)) {
int length = sfzh.length();
if (length == 18) {
boolean bitValid = true;
for (int i = 0; i < 17; i++) {
String bitChar = sfzh.substring(i, i + 1);
if ("1234567890".indexOf(bitChar) == -1) {
bitValid = false;
break;
}
}
if (bitValid) {
java.util.Date csrq = DateUtils.stringToDate(sfzh
.substring(6, 14), "yyyyMMdd");
int lastAt = 0;
for (int i = 0; i < 17; i++) {
int bitInt = Integer.parseInt(sfzh.substring(i,
i + 1));
int bitIntTemp = 1;
for (int j = 0; j < 17 - i; j++) {
bitIntTemp = (bitIntTemp * 2) % 11;
}
lastAt += bitInt * bitIntTemp;
}
lastAt = lastAt % 11;
String checkBit = "10X98765432".substring(lastAt,
lastAt + 1);
String lastBit = sfzh.substring(17);
lastBit = lastBit.toUpperCase();
if (checkBit.equals(lastBit)) {
returnValue = true;
}
}
}
}
} catch (Exception ex) {
// logger.error("18 位身份证号码校验出错: " + ex.getMessage(), ex);
}
return returnValue;
}
/**
* 取得HTML中某个结点替换后的字符串流程定义保存HTML时用<br>
* 说明:搜索到关键字后以<>为前后匹配截取
*
* @param htmlString
* HTML字符串
* @param searchKey
* 搜索关键字
* @param nodeString
* 新的结点字符串
* @return String
*/
public static String getHTMLNodeReplaceString(String htmlString,
String searchKey, String nodeString) {
String returnValue = htmlString;
if (!isBlank(htmlString) && !isBlank(searchKey)) {
int foundI = htmlString.indexOf(searchKey);
if (foundI != -1) {
int startI = htmlString.lastIndexOf("<", foundI);
int endI = htmlString.indexOf(">", foundI);
if (startI != -1 && endI != -1) {
returnValue = htmlString.substring(0, startI)
+ nullToStr(nodeString)
+ htmlString.substring(endI + 1);
}
}
}
return returnValue;
}
/**
* 生成32位的UUID<br>
*
* @return String
*/
public static String get32UUID() {
UUID uuid = UUID.randomUUID();
String returnValue = uuid.toString();
returnValue = returnValue.replace("-", "");
return returnValue;
}
//更具所属责任区生成对应SQL
public static String GenerationSql(String sszrq){
String[] sszrqArray = sszrq.split(",");
StringBuffer sb = new StringBuffer();
sb.append("and (");
for(int i = 0 ; i < sszrqArray.length ; i++ ){
if(null != sszrqArray[i] && !sszrqArray[i].equals("")){
sb.append(" code = '"+ sszrqArray[i] +"' ");
if(i != (sszrqArray.length-1)){
sb.append(" or ");
}
}
}
sb.append(")");
return sb.toString();
}
/**
* 去掉字符串中的空格、回车、换行符、制表符
*
* replaceBlank (这里用一句话描述这个方法的作用)
* TODO(这里描述方法适用条件/执行流程/使用方法/注意事项)
* @author Administrator
* @datetime 2016-5-3 下午4:50:01
* @param str
* @return
* @throws (抛出的异常)
* @since JDK 1.7
*/
public static String replaceBlank(String str) {
if (str != null) {
Pattern ptn = Pattern.compile("\\s*|\t|\r|\n");
Matcher matcher = ptn.matcher(str);
return matcher.replaceAll("");
}
return str;
}
/**
* Base64字符串转码至byte数组
* @param str
* @return
*/
public static byte[] getBase64DecodeByte(String str) {
byte[] returnValue = (byte[])null;
try {
if(!StringUtils.isBlank(str)) {
returnValue = Base64.decodeBase64(str);
}
} catch (Exception e) {
// logger.error("取得byte数组解码后的BASE64出错: " + ex.getMessage(), ex);
}
return returnValue;
}
/**
* byte数组转码至Base64字符串
* @param bytes
* @return
*/
public static String getByteToBase64(byte[] bytes) {
String returnValue = "";
try {
if(bytes != null) {
returnValue = Base64.encodeBase64String(bytes);
}
} catch (Exception e) {
// logger.error("取得byte数组解码后的BASE64出错: " + ex.getMessage(), ex);
}
return returnValue;
}
}