1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.MessageDigest; import java.util.Date; import java.util.Formatter; import java.util.UUID; import org.springframework.lang.Nullable; import org.springframework.util.DigestUtils;
public class StringUtils extends org.springframework.util.StringUtils { public static boolean equals(@Nullable String str1, @Nullable String str2) { if (str1 == null || str2 == null) { return (str1 == null && str2 == null); } return str1.equals(str2); } public static boolean isBlank(@Nullable String string) { if (isEmpty(string)) { return true; } for (int i = 0; i < string.length(); i++) { if (!Character.isWhitespace(string.charAt(i))) { return false; } } return true; }
public static boolean isNotBlank(@Nullable String string) { return !StringUtils.isBlank(string); } public static boolean isBlank(@Nullable Integer id) { return id == null || id == 0; }
public static boolean isNotBlank(@Nullable Integer id) { return id != null && id != 0; }
public static String[] split(@Nullable String string, @Nullable String delimiter, int limit) { if (isEmpty(string) || delimiter == null || limit == 1) { return new String[]{string}; } return string.split(delimiter, limit); } public static String uuid() { return UUID.randomUUID().toString().replace("-", ""); } private static final String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; public static String unid8() { return StringUtils.unid8(null); } private static String unid8(String uuid32) { StringBuffer shortBuffer = new StringBuffer(); if (StringUtils.isBlank(uuid32)) { uuid32 = StringUtils.uuid(); } for (int i = 0; i < 8; i++) { String str = uuid32.substring(i * 4, i * 4 + 4); int x = Integer.parseInt(str, 16); shortBuffer.append(StringUtils.chars[x % 0x3E]); } return shortBuffer.toString(); } public static String unid20() { long ts = new Date().getTime(); return String.format("%d_%s", ts, StringUtils.unid8()); } public static String MD5(String str) { if (str == null) { str = ""; } return DigestUtils.md5DigestAsHex(str.getBytes()); }
public static String SHA1(String str) { if (str == null) { return ""; } try { MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(str.getBytes("UTF-8")); return byteToHex(crypt.digest()); } catch (Exception e) { e.printStackTrace(); } return str; } private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; } public static String urlDecode(String raw) { try { return URLDecoder.decode(raw, "UTF-8"); } catch (UnsupportedEncodingException uee) { throw new IllegalStateException(uee); } }
public static String urlEncode(String str) { try { return URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException uee) { throw new IllegalStateException(uee); } } public static String urlEncode(String str, String en) { try { return URLEncoder.encode(str, en); } catch (UnsupportedEncodingException uee) { throw new IllegalStateException(uee); } } public static String convert(String str, String from, String to) { try { str = new String(str.getBytes(from), to); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } }
|