Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.tinypace.mobistore.util; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Locale; import java.util.Random; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.LocaleResolver; /** * , org.apache.commons.lang3.StringUtils * @author ThinkGem * @version 2013-05-22 */ public class StringUtil extends org.apache.commons.lang3.StringUtils { public static boolean IsEmpty(String str) { if (str == null || str.trim().equals("")) { return true; } else { return false; } } public static String lowerFirst(String str) { if (StringUtil.isBlank(str)) { return ""; } else { return str.substring(0, 1).toLowerCase() + str.substring(1); } } public static String upperFirst(String str) { if (StringUtil.isBlank(str)) { return ""; } else { return str.substring(0, 1).toUpperCase() + str.substring(1); } } /** * ?HTML */ public static String replaceHtml(String html) { if (isBlank(html)) { return ""; } String regEx = "<.+?>"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(html); String s = m.replaceAll(""); return s; } /** * ? * @param str * @param length ? * @return */ public static String abbr(String str, int length) { if (str == null) { return ""; } try { StringBuilder sb = new StringBuilder(); int currentLength = 0; for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) { currentLength += String.valueOf(c).getBytes("GBK").length; if (currentLength <= length - 3) { sb.append(c); } else { sb.append("..."); break; } } return sb.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ""; } /** * ?html * @param str * @param length ? * @return */ public static String rabbr(String str, int length) { return abbr(replaceHtml(str), length); } /** * ?Double */ public static Double toDouble(Object val) { if (val == null) { return 0D; } try { return Double.valueOf(trim(val.toString())); } catch (Exception e) { return 0D; } } /** * ?Float */ public static Float toFloat(Object val) { return toDouble(val).floatValue(); } /** * ?Long */ public static Long toLong(Object val) { return toDouble(val).longValue(); } /** * ?Integer */ public static Integer toInteger(Object val) { return toLong(val).intValue(); } /** * i18n */ public static String getMessage(String code, Object[] args) { LocaleResolver localLocaleResolver = SpringContextHolder.getBean(LocaleResolver.class); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); Locale localLocale = localLocaleResolver.resolveLocale(request); return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale); } /** * ? */ public static String getRemoteAddr(HttpServletRequest request) { String remoteAddr = request.getHeader("X-Real-IP"); if (isNotBlank(remoteAddr)) { remoteAddr = request.getHeader("X-Forwarded-For"); } else if (isNotBlank(remoteAddr)) { remoteAddr = request.getHeader("Proxy-Client-IP"); } else if (isNotBlank(remoteAddr)) { remoteAddr = request.getHeader("WL-Proxy-Client-IP"); } return remoteAddr != null ? remoteAddr : request.getRemoteAddr(); } /** * ?JS??? * @param objectString * row.user.id * !row?'':!row.user?'':!row.user.id?'':row.user.id */ public static String jsGetVal(String objectString) { StringBuilder result = new StringBuilder(); StringBuilder val = new StringBuilder(); String[] vals = split(objectString, "."); for (int i = 0; i < vals.length; i++) { val.append("." + vals[i]); result.append("!" + (val.substring(1)) + "?'':"); } result.append(val.substring(1)); return result.toString(); } /** * *??? * @param len * @return */ public static String RandomString(int len) { final int maxNum = 10; int i; // ?? int count = 0; // ?? char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); while (count < len) { // ????? i = Math.abs(r.nextInt(maxNum)); // ?36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); } public static String RandomNumbString(int len) { final int maxNum = 10; int i; // ?? int count = 0; // ?? char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); while (count < len) { // ????? i = Math.abs(r.nextInt(maxNum)); // ?36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); } public static String GetUuid() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); str = str.replace("-", ""); return str.toUpperCase(); } public static String formatMoney(BigDecimal d) { DecimalFormat mformat = new DecimalFormat(); mformat.setMaximumFractionDigits(2); mformat.setMinimumFractionDigits(2); mformat.setGroupingSize(20); return mformat.format(d); } public static void main(String[] args) { // String aa = "1234567890ABCDEFG"; // System.out.println(aa); // aa = Confuse(aa); // System.out.println(aa); // aa = UnConfuse(aa); // System.out.println(aa); } }