J2EE工具类:StringUtil.java


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;
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值