Created
July 19, 2013 10:56
-
-
Save sunfuze/6038330 to your computer and use it in GitHub Desktop.
去掉标点和空字符,转MD5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.UnsupportedEncodingException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class CharacterUtil { | |
public static String removeSymbol(String chs) { | |
return chs.replaceAll("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]", ""); | |
} | |
public static String replaceBlank(String str) { | |
String result = ""; | |
if(str != null) { | |
Pattern p = Pattern.compile("\\s*|\t|\r|\n"); | |
Matcher m = p.matcher(str); | |
result = m.replaceAll(""); | |
} | |
return result; | |
} | |
public static String handleContent(String content) { | |
return getMD5Str(removeSymbol(replaceBlank(content))); | |
} | |
public static String getMD5Str(String str) { | |
MessageDigest messageDigest = null; | |
try { | |
messageDigest = MessageDigest.getInstance("MD5"); | |
messageDigest.reset(); | |
messageDigest.update(str.getBytes("UTF-8")); | |
} catch (NoSuchAlgorithmException e) { | |
System.out.println("NoSuchAlgorithmException caught!"); | |
System.exit(-1); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
byte[] byteArray = messageDigest.digest(); | |
StringBuffer md5StrBuff = new StringBuffer(); | |
for (int i = 0; i < byteArray.length; i++) { | |
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) | |
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); | |
else | |
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); | |
} | |
return md5StrBuff.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment