此类中封装一些常用的字符串操作(和业务无关)。
来源:互联网 发布:近年来 网络爱国事件 编辑:程序博客网 时间:2024/04/28 03:44
- 来自:http://xuliduo.javaeye.com/blog/196937
- package com.wootion.idp.util;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.security.Security;
- import java.text.SimpleDateFormat;
- import java.text.StringCharacterIterator;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.StringTokenizer;
-
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class StringUtil
- {
-
- public static String DES = "DES";
-
- public static String DESEDE = "DESede";
-
- public static String BLOWFISH = "Blowfish";
-
-
-
-
-
- public static char DATE_SIZE_DAY = 'd';
-
-
-
-
- public static char DATE_SIZE_WEEK = 'w';
-
-
-
-
- public static char DATE_SIZE_MONTH = 'm';
-
-
-
-
- public static char DATE_SIZE_YEAR = 'y';
-
-
-
-
- public static char DATE_SIZE_TOMORROW = 'a';
-
-
-
-
- private StringUtil()
- {
- }
-
- public static String randomName() throws Exception
- {
- String strName = null;
-
- RandomStrg rst = new RandomStrg();
- rst.setCharset("a-zA-Z0-9");
- rst.setLength("17");
- rst.generateRandomObject();
- strName = rst.getRandom() + System.currentTimeMillis();
-
- return strName;
- }
-
-
-
-
-
-
-
- public static String now(String str, java.sql.Date dates)
- {
- if ("".equals(str))
- str = "yyyy-MM-dd";
- java.util.Date date = dates;
- return now(str, date);
- }
-
- public static String randomNum()
- {
- String strName = null;
-
- RandomStrg rst = new RandomStrg();
- rst.setCharset("0-9");
- rst.setLength("6");
- try
- {
- rst.generateRandomObject();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- strName = StringUtil.now("yyyyMMdd", null) + rst.getRandom();
-
- return strName;
- }
-
-
-
-
-
-
-
- public static String now(String str, java.util.Date dates)
- {
- if ("".equals(str))
- str = "yyyy-MM-dd";
- java.text.SimpleDateFormat date = new java.text.SimpleDateFormat(str);
- String currentDate = date.format(dates != null ? dates : thisTime());
- return currentDate;
- }
-
-
-
-
-
- public static java.util.Date thisTime()
- {
- return new Date(System.currentTimeMillis());
- }
-
-
-
-
-
-
-
-
-
- public static String[] split(String source, String delim)
- {
- String[] wordLists;
- if (source == null)
- {
- wordLists = new String[1];
- wordLists[0] = source;
- return wordLists;
- }
- if (delim == null)
- {
- delim = ",";
- }
- StringTokenizer st = new StringTokenizer(source, delim);
- int total = st.countTokens();
- wordLists = new String[total];
- for (int i = 0; i < total; i++)
- {
- wordLists[i] = st.nextToken();
- }
- return wordLists;
- }
-
-
-
-
-
-
-
-
- public static String[] split(String source, char delim)
- {
- return split(source, String.valueOf(delim));
- }
-
-
-
-
-
-
-
- public static String[] split(String source)
- {
- return split(source, ",");
- }
-
-
-
-
-
-
-
-
-
- public static void printStrings(String[] strings, String delim,
- OutputStream out)
- {
- try
- {
- if (strings != null)
- {
- int length = strings.length - 1;
- for (int i = 0; i < length; i++)
- {
- if (strings[i] != null)
- {
- if (strings[i].indexOf(delim) > -1)
- {
- out.write(("/"" + strings[i] + "/"" + delim)
- .getBytes());
- }
- else
- {
- out.write((strings[i] + delim).getBytes());
- }
- }
- else
- {
- out.write("null".getBytes());
- }
- }
- if (strings[length] != null)
- {
- if (strings[length].indexOf(delim) > -1)
- {
- out.write(("/"" + strings[length] + "/"").getBytes());
- }
- else
- {
- out.write(strings[length].getBytes());
- }
- }
- else
- {
- out.write("null".getBytes());
- }
- }
- else
- {
- out.write("null".getBytes());
- }
- out.write(java.lang.Character.LINE_SEPARATOR);
- }
- catch (IOException e)
- {
-
- }
- }
-
-
-
-
-
-
-
-
- public static void printStrings(String[] strings, String delim)
- {
- printStrings(strings, delim, System.out);
- }
-
-
-
-
-
-
-
-
- public static void printStrings(String[] strings, OutputStream out)
- {
- printStrings(strings, ",", out);
- }
-
-
-
-
-
-
-
- public static void printStrings(String[] strings)
- {
- printStrings(strings, ",", System.out);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String getReplaceString(String prefix, String source,
- String[] values)
- {
- String result = source;
- if (source == null || values == null || values.length < 1)
- {
- return source;
- }
- if (prefix == null)
- {
- prefix = "%";
- }
-
- for (int i = 0; i < values.length; i++)
- {
- String argument = prefix + Integer.toString(i + 1);
- int index = result.indexOf(argument);
- if (index != -1)
- {
- String temp = result.substring(0, index);
- if (i < values.length)
- {
- temp += values[i];
- }
- else
- {
- temp += values[values.length - 1];
- }
- temp += result.substring(index + 2);
- result = temp;
- }
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
- public static String getReplaceString(String source, String[] values)
- {
- return getReplaceString("%", source, values);
- }
-
-
-
-
-
-
-
-
-
- public static boolean contains(String[] strings, String string,
- boolean caseSensitive)
- {
- for (int i = 0; i < strings.length; i++)
- {
- if (caseSensitive == true)
- {
- if (strings[i].equals(string))
- {
- return true;
- }
- }
- else
- {
- if (strings[i].equalsIgnoreCase(string))
- {
- return true;
- }
- }
- }
- return false;
- }
-
-
-
-
-
-
-
-
- public static boolean contains(String[] strings, String string)
- {
- return contains(strings, string, true);
- }
-
-
-
-
-
-
-
-
- public static boolean containsIgnoreCase(String[] strings, String string)
- {
- return contains(strings, string, false);
- }
-
-
-
-
-
-
-
-
- public static String combineStringArray(String[] array, String delim)
- {
- int length = array.length - 1;
- if (delim == null)
- {
- delim = "";
- }
- StringBuffer result = new StringBuffer(length * 8);
- for (int i = 0; i < length; i++)
- {
- result.append(array[i]);
- result.append(delim);
- }
- result.append(array[length]);
- return result.toString();
- }
-
-
-
-
-
-
-
-
- public static String fillString(char c, int length)
- {
- String ret = "";
- for (int i = 0; i < length; i++)
- {
- ret += c;
- }
- return ret;
- }
-
-
-
-
-
-
-
- public static String trimLeft(String value)
- {
- String result = value;
- if (result == null)
- return result;
- char ch[] = result.toCharArray();
- int index = -1;
- for (int i = 0; i < ch.length; i++)
- {
- if (Character.isWhitespace(ch[i]))
- {
- index = i;
- }
- else
- {
- break;
- }
- }
- if (index != -1)
- {
- result = result.substring(index + 1);
- }
- return result;
- }
-
-
-
-
-
-
-
- public static String trimRight(String value)
- {
- String result = value;
- if (result == null)
- return result;
- char ch[] = result.toCharArray();
- int endIndex = -1;
- for (int i = ch.length - 1; i > -1; i--)
- {
- if (Character.isWhitespace(ch[i]))
- {
- endIndex = i;
- }
- else
- {
- break;
- }
- }
- if (endIndex != -1)
- {
- result = result.substring(0, endIndex);
- }
- return result;
- }
-
-
-
-
-
-
-
-
- public static String escapeCharacter(String source, HashMap escapeCharMap)
- {
- if (source == null || source.length() == 0)
- return source;
- if (escapeCharMap.size() == 0)
- return source;
- StringBuffer sb = new StringBuffer();
- StringCharacterIterator sci = new StringCharacterIterator(source);
- for (char c = sci.first(); c != StringCharacterIterator.DONE; c = sci
- .next())
- {
- String character = String.valueOf(c);
- if (escapeCharMap.containsKey(character))
- character = (String) escapeCharMap.get(character);
- sb.append(character);
- }
- return sb.toString();
- }
-
-
-
-
-
-
-
- public static int getByteLength(String source)
- {
- int len = 0;
- for (int i = 0; i < source.length(); i++)
- {
- char c = source.charAt(i);
- int highByte = c >>> 8;
- len += highByte == 0 ? 1 : 2;
- }
- return len;
- }
-
-
-
-
-
-
-
-
- public static int getSubtringCount(String source, String sub)
- {
- if (source == null || source.length() == 0)
- {
- return 0;
- }
- int count = 0;
- int index = source.indexOf(sub);
- while (index >= 0)
- {
- count++;
- index = source.indexOf(sub, index + 1);
- }
- return count;
- }
-
-
-
-
-
-
- public static String checkNull(String source)
- {
- return (source = (source == null ? "" : source));
- }
-
-
-
-
-
-
- public static String escape(String src)
- {
- int i;
- char j;
- StringBuffer tmp = new StringBuffer();
- tmp.ensureCapacity(src.length() * 6);
- for (i = 0; i < src.length(); i++)
- {
- j = src.charAt(i);
- if (Character.isDigit(j) || Character.isLowerCase(j)
- || Character.isUpperCase(j))
- {
- tmp.append(j);
- }
- else if (j < 256)
- {
- tmp.append("%");
- if (j < 16)
- {
- tmp.append("0");
- }
- tmp.append(Integer.toString(j, 16));
- }
- else
- {
- tmp.append("%u");
- tmp.append(Integer.toString(j, 16));
- }
- }
- return tmp.toString();
- }
-
-
-
-
-
-
- public static String unescape(String src)
- {
- StringBuffer tmp = new StringBuffer();
- tmp.ensureCapacity(src.length());
- int lastPos = 0 , pos = 0;
- char ch;
- while (lastPos < src.length())
- {
- pos = src.indexOf("%", lastPos);
- if (pos == lastPos)
- {
- if (src.charAt(pos + 1) == 'u')
- {
- ch = (char) Integer.parseInt(src
- .substring(pos + 2, pos + 6), 16);
- tmp.append(ch);
- lastPos = pos + 6;
- }
- else
- {
- ch = (char) Integer.parseInt(src
- .substring(pos + 1, pos + 3), 16);
- tmp.append(ch);
- lastPos = pos + 3;
- }
- }
- else
- {
- if (pos == -1)
- {
- tmp.append(src.substring(lastPos));
- lastPos = src.length();
- }
- else
- {
- tmp.append(src.substring(lastPos, pos));
- lastPos = pos;
- }
- }
- }
- return tmp.toString();
- }
-
-
-
-
-
-
- public static String getHtmlEscape(String str)
- {
- return "<script> document.write(unescape('" + escape(str)
- + "')); </script>";
- }
-
-
-
-
-
-
-
-
- public static String getBettown(String s, String str1, String str2)
- {
- return s.substring(s.indexOf(str1) + 1, s.indexOf(str2));
- }
-
-
-
-
-
-
- public static Date strToDate(String str)
- {
-
- Date date = null;
- try
- {
- SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM-dd");
- date = formatters.parse(str);
-
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return date;
- }
-
-
-
-
-
-
-
-
- public static Date strToDate(String str, String dateformat)
- {
-
- Date date = null;
- try
- {
- SimpleDateFormat formatters = new SimpleDateFormat(dateformat);
- date = formatters.parse(str);
-
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return date;
-
- }
-
-
-
-
-
-
-
-
- public static String dateToStr(Date time, String dateformat)
- {
-
- String strDate = "";
- try
- {
- SimpleDateFormat formatters = new SimpleDateFormat(dateformat);
- strDate = formatters.format(time);
-
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return strDate;
- }
-
-
-
-
-
-
- public static Date dateSize(char t)
- {
-
- Date date = thisTime();
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- switch (t)
- {
- case 'd' :
-
- break;
- case 'w' :
- calendar.roll(calendar.WEEK_OF_YEAR, false);
- break;
- case 'm' :
- calendar.roll(calendar.MONTH, false);
- break;
- case 'y' :
- calendar.roll(calendar.YEAR, false);
- break;
- case 'a' :
- calendar.roll(calendar.DATE, true);
- break;
- default :
- System.out.println("dateSize 函数参数错误");
- }
- date = calendar.getTime();
- return date;
- }
-
-
-
-
-
-
-
- public static String getStringFromArray(String str[])
- throws NullPointerException
- {
- if (str == null)
- throw new NullPointerException("请输入需要转换的数组");
- String strs = "";
- for (String s : str)
- {
- strs += s + ",";
- }
- if (strs.length() > 0)
- strs = strs.substring(0, strs.length() - 1);
- return strs;
- }
-
-
-
-
-
-
- public static String interceptString(String source)
- {
- return interceptString(source, "...", 10);
- }
-
-
-
-
-
-
-
-
- public static String interceptString(String source, String str, int number)
- {
- source = checkNull(source);
- str = str == null ? "..." : str;
-
- int len = strlen(source);
- if (len <= number * 2)
- return source;
- int k = 0;
- for (int i = source.length() - 1; i >= 0; i--)
- {
- char c = source.charAt(i);
- if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
- || (c >= 'A' && c <= 'Z'))
- {
- k = +2;
- }
- else
- {
- if (Character.isLetter(c))
- {
- continue;
- }
- else
- {
-
- continue;
- }
- }
- if (k == 3)
- {
- number++;
- k = 0;
- }
- }
- if (k == 1)
- number++;
- return source.substring(0, number - 1) + str;
- }
-
-
-
-
-
-
- public static int strlen(String str)
- {
- if (str == null || str.length() <= 0)
- {
- return 0;
- }
- int len = 0;
- char c;
- for (int i = str.length() - 1; i >= 0; i--)
- {
- c = str.charAt(i);
- if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
- || (c >= 'A' && c <= 'Z'))
- {
-
- len++;
- }
- else
- {
- if (Character.isLetter(c))
- {
- len += 2;
- }
- else
- {
- len++;
- }
- }
- }
- return len;
- }
-
-
-
-
- static
- {
- Security.addProvider(new com.sun.crypto.provider.SunJCE());
- }
-
-
-
-
-
-
- public static String byte2hex(byte[] b)
- {
- String hs = "";
- String stmp = "";
- for (int n = 0; n < b.length; n++)
- {
- stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
- if (stmp.length() == 1)
- hs = hs + "0" + stmp;
- else
- hs = hs + stmp;
- if (n < b.length - 1)
- hs = hs + ":";
- }
- return hs.toUpperCase();
- }
-
-
-
-
-
-
- public static byte[] hex2byte(String str)
- {
- String stmp[] = str.split(":");
- byte[] btmp = new byte[stmp.length];
- for (int n = 0; n < stmp.length; n++)
- {
- int i = Integer.valueOf(stmp[n], 16);
- btmp[n] = (byte) (i & 0XFF);
- }
- return btmp;
- }
-
-
-
-
-
-
-
- public static byte[] getKey(String algorithm) throws Exception
- {
- KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
- SecretKey deskey = keygen.generateKey();
-
- return deskey.getEncoded();
- }
-
-
-
-
-
-
-
-
-
- public static byte[] encode(byte[] input, byte[] key, String algorithm)
- throws Exception
- {
- SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, algorithm);
-
- Cipher c1 = Cipher.getInstance(algorithm);
- c1.init(Cipher.ENCRYPT_MODE, deskey);
- byte[] cipherByte = c1.doFinal(input);
-
- return cipherByte;
- }
-
-
-
-
-
-
-
-
-
- public static String encode(String input, String key, String algorithm)
- throws Exception
- {
- return byte2hex(encode(input.getBytes(), key.getBytes(), algorithm));
- }
-
-
-
-
-
-
-
-
-
- public static byte[] decode(byte[] input, byte[] key, String algorithm)
- throws Exception
- {
- SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, algorithm);
-
- Cipher c1 = Cipher.getInstance(algorithm);
- c1.init(Cipher.DECRYPT_MODE, deskey);
- byte[] clearByte = c1.doFinal(input);
-
- return clearByte;
- }
-
-
-
-
-
-
-
-
-
- public static String decode(String input, String key, String algorithm)
- throws Exception
- {
- return new String(decode(hex2byte(input), key.getBytes(), algorithm));
- }
-
-
-
-
-
-
-
- public static byte[] md5(byte[] input) throws Exception
- {
- java.security.MessageDigest alg = java.security.MessageDigest
- .getInstance("MD5");
-
- alg.update(input);
- byte[] digest = alg.digest();
-
- return digest;
- }
-
-
-
-
-
-
-
- public static String changeToBig(double value)
- {
- char[] hunit = {'拾', '佰', '仟'};
- char[] vunit = {'万', '亿'};
- char[] digit = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
- long midVal = (long) (value * 100);
- String valStr = String.valueOf(midVal);
- String head = valStr.substring(0, valStr.length() - 2);
- String rail = valStr.substring(valStr.length() - 2);
-
- String prefix = "";
- String suffix = "";
-
- if (rail.equals("00"))
- {
- suffix = "整";
- }
- else
- {
- suffix = digit[rail.charAt(0) - '0'] + "角"
- + digit[rail.charAt(1) - '0'] + "分";
- }
-
- char[] chDig = head.toCharArray();
- char zero = '0';
- byte zeroSerNum = 0;
- for (int i = 0; i < chDig.length; i++)
- {
- int idx = (chDig.length - i - 1) % 4;
- int vidx = (chDig.length - i - 1) / 4;
- if (chDig[i] == '0')
- {
- zeroSerNum++;
- if (zero == '0')
- {
- zero = digit[0];
- }
- else if (idx == 0 && vidx > 0 && zeroSerNum < 4)
- {
- prefix += vunit[vidx - 1];
- zero = '0';
- }
- continue;
- }
- zeroSerNum = 0;
- if (zero != '0')
- {
- prefix += zero;
- zero = '0';
- }
- prefix += digit[chDig[i] - '0'];
- if (idx > 0)
- prefix += hunit[idx - 1];
- if (idx == 0 && vidx > 0)
- {
- prefix += vunit[vidx - 1];
- }
- }
-
- if (prefix.length() > 0)
- prefix += '圆';
-
- return prefix + suffix;
- }
-
-
-
-
-
-
- public static String toUtf8String(String s)
- {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
- if (c >= 0 && c <= 255)
- {
- sb.append(c);
- }
- else
- {
- byte[] b;
- try
- {
- b = Character.toString(c).getBytes("utf-8");
- }
- catch (Exception ex)
- {
- System.out.println(ex);
- b = new byte[0];
- }
- for (int j = 0; j < b.length; j++)
- {
- int k = b[j];
- if (k < 0)
- k += 256;
- sb.append("%" + Integer.toHexString(k).toUpperCase());
- }
- }
- }
- return sb.toString();
- }
-
-
-
-
-
-
- public static String getSpaces(int num)
- {
- StringBuffer sbr = new StringBuffer();
- for (int i = 0; i < num; i++)
- {
- sbr.append(" ");
- }
- return sbr.toString();
- }
-
- }