package cn.rong.testdwr.commons;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Date;
import java.util.Map;
/**
* <p>
* Title:
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2012
* </p>
* <p>
* Company:
* </p>
*
* @author
* @version 1.0
*/
public class StringUtil {
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
/**
* 转换字节数组为16进制字串
*
* @param b 字节数组
* @return 16进制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
/**
*
* @param origin
* @return 返回经过MD5加密的字符串
*/
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(md.digest(resultString
.getBytes("UTF-8")));
} catch (Exception ex) {
}
return resultString;
}
/**
*
* @param s
* @param strCheck
* @return 判断s中是否含有strCheck中的串
*/
public static boolean inStr(String s, String[] strCheck) {
boolean result = false;
for (String check : strCheck)
result = result || (s.trim().indexOf(check.trim()) > -1);
return result;
}
/**
*
* @param file
* @param split
* @return 按照split分隔符取得文件的类型
*/
public static String getFileType(String file, String split) {
String in = file.trim();
String type = in.substring(in.lastIndexOf(split) + 1, in.length());
return type;
}
/**
*
* @param file
* @param split
* @return按照split分隔符取得文件的名字
*/
public static String getFileName(String file, String split) {
String in = file.trim();
String type = in.substring(0, in.lastIndexOf(split));
return type;
}
/**
*
* @param s
* @param strcheck
* @return 检测s是否为待检验的类型
*/
public static boolean fileTypeChecker(String s, String strcheck) {
boolean result = false;
String type = getFileType(s, ".");
String[] strCheck = splitStr(strcheck, ",");
for (String check : strCheck)
result = result || type.equals(check.trim());
return result;
}
/**
*
* @param strInput
* @param strCheck
* @return 检验strInput串中是否含有strCheck中的串
*/
public static boolean checkStr(String[] strInput, String[] strCheck) {
boolean result = false;
for (String input : strInput)
result = result || inStr(input, strCheck);
return result;
}
/**
*
* @param str
* @param length
* @param insertStr
* @return 按照长度在某一段字符串中插入另一段字符
*/
public static String insertStr(String str, int length, String insertStr) {
String result = "";
int total = str.length();
int i = 0;
while (true) {
if ((i + length) < total)
result = result + str.substring(i, i + length) + insertStr;
else {
result = result + str.substring(i, total);
break;
}
i = i + length;
}
return result;
}
/**
*
* @param s
* @param strMap
* @return 把s中的字符按照map中的对应字符进行替换
*/
public static String replaceStr(String s, Map<String, String> strMap) {
for (Map.Entry<String, String> entry : strMap.entrySet()) {
s = s.replaceAll(entry.getKey(), entry.getValue());
}
return s;
}
/**
*
* @param str
* @param length
* @param r
* @return 切断字符串
*/
public static String getStrByLength(String str, int length, String r) {
if (str.length() < length)
return str;
String newStr = str.substring(0, length) + r;
return newStr;
}
public static String[] splitStr(String s, String sp) {
return s.split(sp);
}
/**
*
* @param s
* @param sp
* @param type
* @return 把一组数字字符串转换为数字数组
*/
public static int[] splitStr(String s, String sp, int type) {
String[] intStr = splitStr(s, sp);
int[] result = new int[intStr.length];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt(intStr[i]);
}
return result;
}
public static String encodeStr(String input, String sourceEncode,
String targetEncode) {
String result;
try {
result = new String(input.getBytes(sourceEncode), targetEncode);
} catch (UnsupportedEncodingException e) {
result = input;
}
return result;
}
public static void main(String[] args) {
// System.err.println(MD5Encode("NoPassword"));
// System.err.println(inStr("a",new String[]{"b","b"}));
// Map<String,String> strMap=new HashMap<String,String>();
// strMap.put("fuck", "[-bi-]");
// strMap.put("逼", "[-呵呵-]");
// System.out.println(replaceStr("fuck you 逼!!",strMap));
// for(int i:splitStr("1,232,2312,231,312",",",1))System.out.println(i);
System.out.println(getFileName("ab.txt", "."));
System.out.println(MD5Encode(new Date().toString()));
}
}
- 1
- 2
前往页