package com.amway.ebiz.common.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import com.amway.ebiz.common.constanst.Constansts;import com.amway.ebiz.common.exception.EbizException;import com.amway.ebiz.common.util.cache.CacheUtil;/** * 字符串工具 * * @author andy Wang */public class StringUtil {/** * 判断字符串是否为null或空字符串 * * @param s * @return */public static boolean isBlank(String s) {return s == null || s.trim().length() == 0;}/** * trim a string * * @param s * @return */public static String trim(String s) {return s == null ? "" : s.trim();}/*** * 判断字符串长度,其中一个中文算2位 * * @param source * @return int * @throws UnsupportedEncodingException */public static int length(String source) throws UnsupportedEncodingException {if (source == null || source.length() == 0) {return 0;}source = new String(source.getBytes(), "ISO8859_1");return source.length();}/** * 将对象转化为String * * @param obj * @return String */public static String toString(Object obj) {String str = "";if (obj != null) {str = String.valueOf(obj);if ("null".equals(str)) {str = "";}}return str;} public static String mergeStringsFlushRight(String backGround, String fore){if(backGround == null || fore == null){return null;}if(fore.length() >= backGround.length()){return fore;}String result = backGround.substring(0,backGround.length()-fore.length()) + fore;return result;} /** * 过滤字符串中所有全角和半角空格 * * @param strInput * 需要过滤空格的字符串 * @return String * @throws Exception * @author 陈磊 */ public static String trimBlank(String strInput) throws Exception { //过滤半角空格 while (strInput.indexOf(" ") != -1) { strInput = strInput.replaceAll(" ", ""); } //过滤全角空格 while (strInput.indexOf(" ") != -1) { strInput = strInput.replaceAll(" ", ""); } return strInput; } /** * 将字符串补0 * * @param str * 字符串 * @param length * 长度,如果字符串长度小于length,其它的位置补0 * @return 补0后的字符串 */ public static String fillString(String str, int length) { return fillString(str, length, '0'); } /** * 将字符串补指定字符 * * @param str * 字符串 * @param length * 长度,如果字符串长度小于length,其它的位置补ch * @param ch * 补充字符 * @return 补0后的字符串 */ public static String fillString(String str, int length, char ch) { if (str == null || str.length() == 0 || str.length() >= length) { return str; } int size = length - str.length(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < size; i++) { buffer.append(ch); } buffer.append(str); return buffer.toString(); } /** * 将YYYY-MM-DD HH:mm:ss 转换为YYYYMMDDhhmmss * @param dateTimeStr * @return String */ public static String formatdateTimeString(String dateTimeStr){ if(dateTimeStr == null || dateTimeStr.trim().length() == 0){ return ""; } return dateTimeStr.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", ""); } /** * 根据缓存配置,去掉配置上的特别字符 * @param str * */ public static String stringFilter(String str){ if(!isBlank(str)){ String filter=CacheUtil.getSystemConfig().getConfig( Constansts.EORDER_SETTING, "PRODUCT_NAME_FILTER"); Pattern p = Pattern.compile(filter); Matcher m = p.matcher(str); return m.replaceAll("").trim(); }else{ return str; } } /** * 将字节流转换成字符串,charSet不能为空,必须指定 * @param is * @param charSet 编码 * */ public static String stream2String(final InputStream is, String charSet) throws IOException { if (null == is || StringUtil.isBlank(charSet)) { return null; } BufferedReader reader = null; StringBuffer sb = null; try { reader = new BufferedReader(new InputStreamReader(is, charSet)); sb = new StringBuffer(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } }catch(Exception e){ e.printStackTrace(); } finally { if (null != reader) { reader.close(); } } if (null == sb) { return null; } return sb.toString(); } /** * 退款单号 * 银联、支付宝一致 * 返回字符串:20130325HALHQAHTH303H995H1 * @param tradeNo * @param tradeTime * @return String * @throws EbizException */ public static String getRefundNo(String tradeNo,String tradeTime) throws EbizException{ if(isBlank(tradeTime) || tradeTime.length() < 8){ throw new EbizException("getRefundNo:invalidate tradeTime"); } //tradeNo = tradeNo.replaceAll("-", "H"); tradeNo = tradeTime.substring(0, 8)+"H"+tradeNo; return tradeNo; } /** * 转换退款订单号为支付订单号 * @param refOrderNumber * @return */ public static String convertRefundNo(String refOrderNumber){ if(!isBlank(refOrderNumber)){ String refundPfx = refOrderNumber.substring(0, 8); if(isNumeric(refundPfx)){ return refOrderNumber.substring(9,refOrderNumber.length()); } } return refOrderNumber; } /** * 判断字符串是否是数字 * @param str * @return */public static boolean isNumeric(String str) {Pattern pattern = Pattern.compile("[0-9]*");Matcher isNum = pattern.matcher(str);if (!isNum.matches()) {return false;}return true;} /** * 格式化数字 * @param numberFormat * @param v * @return * @author CNVISD61 * @date 2013-7-23 */public static String numberFormat(String numberFormat, Object v) {DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance();formatter.applyPattern(numberFormat);return formatter.format(v);}/** * 转换propertyName到setter方法。 * 例如,propertyName为"title",则通过此方法转换后返回"setTitle" * @param propertyName * @return */public static String convert2Setter(String propertyName){String ret = null;if(propertyName!=null && !propertyName.equals("")){ret = "set"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1);}return ret;}/** * 参数名称:@param dateStr 待转换的日期(String格式,结构为yyyyMMdd) * 返回值:String 小写汉字的日期 */public static String convertDateCh(String dateStr) { String resultStr = ""; String[] ss = dateStr.split("-"); for (int j = 0; j < ss[0].length(); j++) { switch (ss[0].charAt(j)) { case '0': resultStr += "零"; break; case '1': resultStr += "一"; break; case '2': resultStr += "二"; break; case '3': resultStr += "三"; break; case '4': resultStr += "四"; break; case '5': resultStr += "五"; break; case '6': resultStr += "六"; break; case '7': resultStr += "七"; break; case '8': resultStr += "八"; break; case '9': resultStr += "九"; break; } } resultStr = resultStr + "年"; if (ss[1].equals("01")) { resultStr += "一"; } else if (ss[1].equals("02")) { resultStr += "二"; } else if (ss[1].equals("03")) { resultStr += "三"; } else if (ss[1].equals("04")) { resultStr += "四"; } else if (ss[1].equals("05")) { resultStr += "五"; } else if (ss[1].equals("06")) { resultStr += "六"; } else if (ss[1].equals("07")) { resultStr += "七"; } else if (ss[1].equals("08")) { resultStr += "八"; } else if (ss[1].equals("09")) { resultStr += "九"; } else if (ss[1].equals("10")) { resultStr += "十"; } else if (ss[1].equals("11")) { resultStr += "十一"; } else { resultStr += "十二"; } resultStr += "月"; if (ss[2].equals("01")) { resultStr += "一"; } else if (ss[2].equals("02")) { resultStr += "二"; } else if (ss[2].equals("03")) { resultStr += "三"; } else if (ss[2].equals("04")) { resultStr += "四"; } else if (ss[2].equals("05")) { resultStr += "五"; } else if (ss[2].equals("06")) { resultStr += "六"; } else if (ss[2].equals("07")) { resultStr += "七"; } else if (ss[2].equals("08")) { resultStr += "八"; } else if (ss[2].equals("09")) { resultStr += "九"; } else if (ss[2].equals("10")) { resultStr += "十"; } else if (ss[2].equals("11")) { resultStr += "十一"; } else if (ss[2].equals("12")) { resultStr += "十二"; } else if (ss[2].equals("13")) { resultStr += "十三"; } else if (ss[2].equals("14")) { resultStr += "十四"; } else if (ss[2].equals("15")) { resultStr += "十五"; } else if (ss[2].equals("16")) { resultStr += "十六"; } else if (ss[2].equals("17")) { resultStr += "十七"; } else if (ss[2].equals("18")) { resultStr += "十八"; } else if (ss[2].equals("19")) { resultStr += "十九"; } else if (ss[2].equals("20")) { resultStr += "二十"; } else if (ss[2].equals("21")) { resultStr += "二十一"; } else if (ss[2].equals("22")) { resultStr += "二十二"; } else if (ss[2].equals("23")) { resultStr += "二十三"; } else if (ss[2].equals("24")) { resultStr += "二十四"; } else if (ss[2].equals("25")) { resultStr += "二十五"; } else if (ss[2].equals("26")) { resultStr += "二十六"; } else if (ss[2].equals("27")) { resultStr += "二十七"; } else if (ss[2].equals("28")) { resultStr += "二十八"; } else if (ss[2].equals("29")) { resultStr += "二十九"; } else if (ss[2].equals("30")) { resultStr += "三十"; } else if (ss[2].equals("31")) { resultStr += "三十一"; } resultStr += "日"; return resultStr;}/** * 转换金额为中文大写 * @param d * @return * @author CNVISD61 * @date 2014-6-17 */public static String double2chmoney(double d){final String [] UNITS1 = {"仟","佰","拾","万","仟","佰","拾","元","角","分"};final String [] UNITS2 = {"佰","拾","万","仟","佰","拾","元","角","分"};//位数int bit = 10;String [] units = UNITS1;String [] chNumbers = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};// 如果小于一千万if(d < 10000000.00){bit = 9;units = UNITS2;}//数位数组int [] ints = new int[bit];String [] strings = new String[bit];String text = "";//除数long divisor = 10L;//被除数//bycobe 20100924 BigDecimal精确度问题,sumIncome=52628.81时获取其值为52628.8099999999976716935634613037109375//因此进行四舍五入处理 //long dividend = sumIncome.movePointRight(2).longValue(); long dividend = Math.round(d * 100); int i = bit - 1;boolean hasSigned = false; //币种标志 //将原金额拆分成整形数组while(i >= 0){ints[i] = (int) (dividend % divisor);dividend = dividend / 10;i--;}//将整型数组转成字符串数组,并加上币种符号if(ints[0] > 0)hasSigned = true;for(i = 0; i < bit; i++){if(ints[i] > 0 || hasSigned){strings[i] = String.valueOf(ints[i]);if(i > 0 && !hasSigned){strings[i-1] = "¥";hasSigned = true;}}else{strings[i] = "";}}//从整型数组生成中文大写金额字符串for(i = 0; i < bit; i++){if(i < bit - 2){text += chNumbers[ints[i]];text += units[i];}else{if(ints[bit - 2] != 0 && ints[bit - 1] != 0){text += chNumbers[ints[i]];text += units[i];}else if(ints[bit - 2] == 0 && ints[bit - 1] != 0){if(i == bit - 2){text += chNumbers[ints[i]];}else if(i == bit - 1){text += chNumbers[ints[i]];text += units[i];}}else if(ints[bit - 2] != 0 && ints[bit - 1] == 0){if(i == bit - 2){text += chNumbers[ints[i]];text += units[i];}}}}boolean b = true;while(b){b = false;if(text.indexOf("零亿") != -1){text = text.replaceAll("零亿", "亿");b = true;}else if(text.indexOf("零万") != -1){text = text.replaceAll("零万", "万");b = true;}else if(text.indexOf("零仟") != -1){text = text.replaceAll("零仟", "零");b = true;}else if(text.indexOf("零佰") != -1){text = text.replaceAll("零佰", "零");b = true;}else if(text.indexOf("零拾") != -1){text = text.replaceAll("零拾", "零");b = true;}else if(text.indexOf("零元") != -1){text = text.replaceAll("零元", "元");b = true;}else if(text.indexOf("零零") != -1){text = text.replaceAll("零零", "零");b = true;}else if(!text.equals("零元") && text.indexOf("零")==0){text = text.substring(1, text.length());b = true;}else if(text.indexOf("亿")==0){text = text.substring(1, text.length());b = true;}else if(text.indexOf("万")==0){text = text.substring(1, text.length());b = true;}else if(text.indexOf("元")==0){text = text.substring(1, text.length());b = true;}}if(ints[bit-2] == 0 && ints[bit-1] == 0){text += "整";}if(text.equals("整")){text = "零元整";}return text;} /** * 生成省略的用户名和ada * @param masterName * @param spourseName * @param ada * @return */public static String getOmitInfo(String masterName, String spourseName, String ada) {String result = "";int length = 0;if(!StringUtil.isBlank(masterName)){length = masterName.length();if(length > 3){result += masterName.substring(0,2) + createOmit(length-2)+" ";} else {result += masterName.substring(0,1) + createOmit(length-1)+" ";}}if(!StringUtil.isBlank(spourseName)){length = spourseName.length();if(length > 3){result += spourseName.substring(0,2) + createOmit(length-2)+" ";} else {result += spourseName.substring(0,1) + createOmit(length-1)+" ";}}if(!StringUtil.isBlank(ada)){length = ada.length();if(length > 4){result += "("+ createOmit(length-3) + ada.substring(length-3) + ")";} else {result += "("+createOmit(length-2) + ada.substring(length-2) + ")";}}return result;}/** * 用*替代 * @param length * @return */public static String createOmit(int length) {String code = "";for (int i = 0; i < length; i++) {code += "*";}return code;}/** * 用指定的分隔符分割指定的字符串 * @return * List 集合 * @author huangbo * 2014-07-21 */public static List excisionStr(String str,String separator){List res = new ArrayList();if(isBlank(str) || isBlank(separator)){return res;}String[] strs = str.split(separator);res.addAll(Arrays.asList(strs));return res;}/** * 将传如来的以元为单位的数值转换为分为单位的数值 * @return * String * @author matt.liu * 2014-11-27 */public static String centTuner(String tradeAmount){String[] tm = tradeAmount.split("\\.");if(tm.length==2){if(tm[1].length() == 2){return String.valueOf(Integer.parseInt(tm[0]) * 100 + Integer.parseInt(tm[1]));}else{return String.valueOf(Integer.parseInt(tm[0]) * 100 + Integer.parseInt(tm[1]) * 10);}//return String.valueOf(Integer.parseInt(tm[0]) * 100 + Integer.parseInt(tm[1]));}else if(tm.length==1){return String.valueOf(Integer.parseInt(tm[0]) * 100);}else{return tradeAmount;}}}
String 帮助类
最新推荐文章于 2024-10-07 08:12:03 发布