package com.worthtech.app.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class StringUtil {
private static MessageDigest digest = null;
/**
* 对字符串进行MD5加密
* @param data
* @return
*/
public String hash(String data) {
if (digest == null)
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
System.err.println("Failed to load the MD5 MessageDigest. We will be unable to function normally.");
nsae.printStackTrace();
}
digest.update(data.getBytes());
return encodeHex(digest.digest());
}
private String encodeHex(byte bytes[]) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] & 0xff) < 16)
buf.append("0");
buf.append(Long.toString(bytes[i] & 0xff, 16));
}
return buf.toString().toUpperCase();
}
/**
* 在list中查询以prefix开头的子项,返回符合匹配的list
* @param list
* @param prefix
* @return
*/
public List getMatches(List list,String prefix) {
String prefix_upper = prefix.toUpperCase();
List matches = new ArrayList();
Iterator iter = list.iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
String name_upper = name.toUpperCase();
if (name_upper.startsWith(prefix_upper)) {
matches.add(name);
}
}
return matches;
}
/**
* 统计字符出现次数
* @param s
*/
public Map<Character, Integer> getStatistics(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
int i, size = s.length();
for (i = 0; i < size; i++) {
char c = s.charAt(i);
map.put(c, map.containsKey(c) ? ((Integer) map.get(c) + 1) : 1);
}
return map;
}
/**
* 将一字符串数组用指定的分隔符接起来
* @param array
* @param split
* @return
*/
public String addSplit(String[] array, String split) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length - 1; i++)
sb.append(array[i] + split);
sb.append(array[array.length - 1]);
return sb.toString();
}
public static void main(String[] args){
// StringUtil util=new StringUtil();
}
/**
* Encode string to hex string, hex string can use in url
* @param str
* @return hex string
*/
public static String HexEncode(String str) {
String hexString = null;
if (str != null && str.length() > 0) {
char[] digital = "0123456789ABCDEF".toCharArray();
StringBuffer sb = new StringBuffer("");
try {
byte[] bs = str.getBytes("utf-8");
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(digital[bit]);
bit = bs[i] & 0x0f;
sb.append(digital[bit]);
}
} catch(Exception e) {
}
hexString = sb.toString();
}
return hexString;
}
/**
* Decode hex string
* @param hexString
* @return
*/
public static String HexDecode(String hexString) {
String str = null;
if (hexString != null && hexString.length() > 0) {
String digital = "0123456789ABCDEF";
char[] hex2char = hexString.toCharArray();
byte[] bytes = new byte[hexString.length() / 2];
int temp;
for (int i = 0; i < bytes.length; i++) {
temp = digital.indexOf(hex2char[2 * i]) * 16;
temp += digital.indexOf(hex2char[2 * i + 1]);
bytes[i] = (byte)(temp & 0xff);
}
try {
str = new String(bytes, "utf-8");
} catch (Exception e) {
}
}
return str;
}
/**
* judge a string is null or empty
* @param str
* @return return true if str is null or empty, otherwise false
*/
public static boolean isEmpty(String str) {
return (str == null || str.length() == 0);
}
/**
*将多余的文字用...代替(length为双字节,比如length=18,表示9个汉字)
*/
public static String getLimitLengthString(String str, int length) {
String view = null;
int counterOfDoubleByte = 0;
byte b[];
if (str == null) {
return "";
} else {
try {
b = str.getBytes("GBK");
if (b.length <= length)
return str;
for (int i = 0; i < length; i++) {
if (b[i] > 0)
counterOfDoubleByte++;
}
if (counterOfDoubleByte % 2 == 0)
view = new String(b, 0, length, "GBK") + "...";
else
view = new String(b, 0, length - 1, "GBK") + "...";
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
}
//身份证效验规则
//http://since2006.com/blog/chinese-id-verify/
public boolean isIdCard(String arrIdCard) {
int sigma = 0;
Integer[] a = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
String[] w = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
for (int i = 0; i < 17; i++) {
int ai = Integer.parseInt(arrIdCard.substring(i, i + 1));
int wi = a[i];
sigma += ai * wi;
}
int number = sigma % 11;
String check_number = w[number];
//return check_number;
//System.out.println(check_number);
if (!arrIdCard.substring(17).equals(check_number)) {
return false;
} else {
return true;
}
}
}
J2EE工具类:StringUtil.java
最新推荐文章于 2024-09-29 13:16:47 发布