| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- package com.cyksj.common.util;
- import cn.hutool.core.util.RandomUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @author chan 字符串操作辅助类
- **/
- public final class StringUtil {
- /**
- * Linux and macOS.
- */
- private static final String LF = "\n";
- /**
- * Classic macOS.
- */
- private static final String CR = "\r";
- /**
- * Windows.
- */
- private static final String CRLF = "\r\n";
- /**
- * An empty immutable {@code String} array.
- */
- public static final String[] EMPTY_STRING_ARRAY = new String[0];
- public static final String EMPTY = "";
- public static final String randomStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#&/_-";
- private static final String baseStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- /**
- * provate constructor
- */
- private StringUtil() { }
- // Empty checks
- //-----------------------------------------------------------------------
- /**
- * <p>Checks if a CharSequence is empty ("") or null.</p>
- *
- * <pre>
- * Strings.isEmpty(null) = true
- * Strings.isEmpty("") = true
- * Strings.isEmpty(" ") = false
- * Strings.isEmpty("bob") = false
- * Strings.isEmpty(" bob ") = false
- * </pre>
- *
- * <p>NOTE: This method changed in Lang version 2.0.
- * It no longer trims the CharSequence.
- * That functionality is available in isBlank().</p>
- *
- * @param cs the CharSequence to check, may be null
- * @return {@code true} if the CharSequence is empty or null
- */
- public static boolean isEmpty(final String cs) {
- return cs == null || cs.isEmpty();
- }
- /**
- * <p>Checks if a CharSequence is not empty ("") and not null.</p>
- *
- * <pre>
- * Strings.isNotEmpty(null) = false
- * Strings.isNotEmpty("") = false
- * Strings.isNotEmpty(" ") = true
- * Strings.isNotEmpty("bob") = true
- * Strings.isNotEmpty(" bob ") = true
- * </pre>
- *
- * @param cs the CharSequence to check, may be null
- * @return {@code true} if the CharSequence is not empty and not null
- */
- public static boolean isNotEmpty(final String cs) {
- return !isEmpty(cs);
- }
- /**
- * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
- *
- * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
- *
- * <pre>
- * Strings.isBlank(null) = true
- * Strings.isBlank("") = true
- * Strings.isBlank(" ") = true
- * Strings.isBlank("bob") = false
- * Strings.isBlank(" bob ") = false
- * </pre>
- *
- * @param cs the CharSequence to check, may be null
- * @return {@code true} if the CharSequence is null, empty or whitespace only
- */
- public static boolean isBlank(final String cs) {
- return cs == null || cs.isBlank();
- }
- /**
- * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
- *
- * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
- *
- * <pre>
- * Strings.isNotBlank(null) = false
- * Strings.isNotBlank("") = false
- * Strings.isNotBlank(" ") = false
- * Strings.isNotBlank("bob") = true
- * Strings.isNotBlank(" bob ") = true
- * </pre>
- *
- * @param cs the CharSequence to check, may be null
- * @return {@code true} if the CharSequence is
- * not empty and not null and not whitespace only
- */
- public static boolean isNotBlank(final String cs) {
- return !isBlank(cs);
- }
- /**
- * <p>Compares two CharSequences, returning {@code true} if they represent
- * equal sequences of characters.</p>
- *
- * <p>{@code null}s are handled without exceptions. Two {@code null}
- * references are considered to be equal. The comparison is <strong>case sensitive</strong>.</p>
- *
- * <pre>
- * StringUtils.equals(null, null) = true
- * StringUtils.equals(null, "abc") = false
- * StringUtils.equals("abc", null) = false
- * StringUtils.equals("abc", "abc") = true
- * StringUtils.equals("abc", "ABC") = false
- * </pre>
- *
- * @param cs1 the first CharSequence, may be {@code null}
- * @param cs2 the second CharSequence, may be {@code null}
- * @return {@code true} if the CharSequences are equal (case-sensitive), or both {@code null}
- */
- public static boolean equals(final String cs1, final String cs2) {
- if (cs1 == null && cs2 == null) {
- return true;
- }
- if (cs1 == null || cs2 == null) {
- return false;
- }
- if (cs1.length() != cs2.length()) {
- return false;
- }
- return cs1.equals(cs2);
- }
- /**
- * <p>Compares two CharSequences, returning {@code true} if they represent
- * equal sequences of characters, ignoring case.</p>
- *
- * <p>{@code null}s are handled without exceptions. Two {@code null}
- * references are considered equal. The comparison is <strong>case insensitive</strong>.</p>
- *
- * <pre>
- * StringUtils.equalsIgnoreCase(null, null) = true
- * StringUtils.equalsIgnoreCase(null, "abc") = false
- * StringUtils.equalsIgnoreCase("abc", null) = false
- * StringUtils.equalsIgnoreCase("abc", "abc") = true
- * StringUtils.equalsIgnoreCase("abc", "ABC") = true
- * </pre>
- *
- * @param cs1 the first CharSequence, may be {@code null}
- * @param cs2 the second CharSequence, may be {@code null}
- * @return {@code true} if the CharSequences are equal (case-insensitive), or both {@code null}
- */
- public static boolean equalsIgnoreCase(final String cs1, final String cs2) {
- if (cs1 == null && cs2 == null) {
- return true;
- }
- if (cs1 == null || cs2 == null) {
- return false;
- }
- if (cs1.length() != cs2.length()) {
- return false;
- }
- return cs1.equalsIgnoreCase(cs2);
- }
- /**
- * <p>Check if a CharSequence starts with a specified prefix.</p>
- *
- * <p>{@code null}s are handled without exceptions. Two {@code null}
- * references are considered to be equal. The comparison is case sensitive.</p>
- *
- * <pre>
- * StringUtils.startsWith(null, null) = true
- * StringUtils.startsWith(null, "abc") = false
- * StringUtils.startsWith("abcdef", null) = false
- * StringUtils.startsWith("abcdef", "abc") = true
- * StringUtils.startsWith("ABCDEF", "abc") = false
- * </pre>
- *
- * @see String#startsWith(String)
- * @param str the CharSequence to check, may be null
- * @param prefix the prefix to find, may be null
- * @return {@code true} if the CharSequence starts with the prefix, case sensitive, or
- * both {@code null}
- */
- public static boolean startsWith(final String str, final String prefix) {
- if (null == str && prefix == null) {
- return true;
- }
- if (null == str || prefix == null) {
- return false;
- }
- return str.startsWith(prefix);
- }
- /**
- * <p>Check if a CharSequence ends with a specified suffix (optionally case insensitive).</p>
- *
- * @see String#endsWith(String)
- * @param str the CharSequence to check, may be null
- * @param suffix the suffix to find, may be null
- * @return {@code true} if the CharSequence starts with the prefix or
- * both {@code null}
- */
- public static boolean endsWith(final String str, final String suffix) {
- if (null == str && suffix == null) {
- return true;
- }
- if (null == str || suffix == null) {
- return false;
- }
- return str.endsWith(suffix);
- }
- /**
- * Performs the logic for the {@code split} and
- * {@code splitPreserveAllTokens} methods that do not return a
- * maximum array length.
- *
- * @param str the String to parse, may be {@code null}
- * @param separatorChar the separate character
- * @return an array of parsed Strings, {@code null} if null String input
- */
- public static String[] split(final String str, final char separatorChar) {
- // Performance tuned for 2.0 (JDK1.4)
- if (str == null) {
- return null;
- }
- final int len = str.length();
- if (len == 0) {
- return EMPTY_STRING_ARRAY;
- }
- final List<String> list = new ArrayList<>();
- int i = 0, start = 0;
- boolean match = false;
- while (i < len) {
- if (str.charAt(i) == separatorChar) {
- if (match) {
- list.add(str.substring(start, i));
- match = false;
- }
- start = ++i;
- continue;
- }
- match = true;
- i++;
- }
- if (match) {
- list.add(str.substring(start, i));
- }
- return list.toArray(new String[list.size()]);
- }
- /**
- * Performs the logic for the {@code split} and
- * {@code splitPreserveAllTokens} methods that return a maximum array
- * length.
- *
- * @param str the String to parse, may be {@code null}
- * @param separatorChars the separate character
- * @return an array of parsed Strings, {@code null} if null String input
- */
- public static String[] split(final String str, final String separatorChars) {
- // Performance tuned for 2.0 (JDK1.4)
- // Direct code is quicker than StringTokenizer.
- // Also, StringTokenizer uses isSpace() not isWhitespace()
- if (str == null) {
- return null;
- }
- final int len = str.length();
- if (len == 0) {
- return EMPTY_STRING_ARRAY;
- }
- final List<String> list = new ArrayList<>();
- int sizePlus1 = 1;
- int i = 0, start = 0;
- boolean match = false;
- final int max = -1;
- if (separatorChars == null) {
- // Null separator means use whitespace
- while (i < len) {
- if (Character.isWhitespace(str.charAt(i))) {
- if (match) {
- if (sizePlus1++ == max) {
- i = len;
- }
- list.add(str.substring(start, i));
- match = false;
- }
- start = ++i;
- continue;
- }
- match = true;
- i++;
- }
- } else if (separatorChars.length() == 1) {
- // Optimise 1 character case
- final char sep = separatorChars.charAt(0);
- while (i < len) {
- if (str.charAt(i) == sep) {
- if (match) {
- if (sizePlus1++ == max) {
- i = len;
- }
- list.add(str.substring(start, i));
- match = false;
- }
- start = ++i;
- continue;
- }
- match = true;
- i++;
- }
- } else {
- // standard case
- while (i < len) {
- if (separatorChars.indexOf(str.charAt(i)) >= 0) {
- if (match) {
- if (sizePlus1++ == max) {
- i = len;
- }
- list.add(str.substring(start, i));
- match = false;
- }
- start = ++i;
- continue;
- }
- match = true;
- i++;
- }
- }
- if (match) {
- list.add(str.substring(start, i));
- }
- return list.toArray(new String[list.size()]);
- }
- /**
- * @author chan
- * @param obj 需要转换的对象
- * @return string
- **/
- public static String getString(Object obj) {
- if (obj == null) {
- return EMPTY;
- } else {
- return obj.toString();
- }
- }
- /**
- * @author chan 异常栈字符串输出
- * @param throwable ex
- * @return String
- **/
- public static String getErrorText(Throwable throwable) {
- if (throwable == null) {
- return "ERROR,throwable is NULL!";
- }
- try (StringWriter strWriter = new StringWriter(512); PrintWriter writer = new PrintWriter(strWriter)) {
- throwable.printStackTrace(writer);
- StringBuffer sb = strWriter.getBuffer();
- return sb.toString();
- } catch (Exception ex) {
- return "ERROR!";
- }
- }
- public static String getErrorMsg(Throwable throwable) {
- if (throwable == null) {
- return "ERROR,throwable is NULL!";
- }
- String message = throwable.getMessage();
- return message;
- }
- /**
- * 随即获取length随机字符串
- */
- public static void getRandomStr(StringBuilder sb, Integer length) {
- Integer baseLength = randomStr.length();
- for (int i = 0; i < length; i++) {
- int number = RandomUtil.randomInt(baseLength);
- sb.append(randomStr.charAt(number));
- }
- }
- public static String randomString(Integer length) {
- if (length == null || length < 0) {
- length = 6;
- }
- Integer baseLength = baseStr.length();
- StringBuilder sb = new StringBuilder(length);
- for (int i = 0; i < length; i++) {
- int number = RandomUtil.randomInt(baseLength);
- sb.append(randomStr.charAt(number));
- }
- return sb.toString();
- }
- /**
- * 生成4位\6位随机数
- */
- public static String getRandomCodeStr(Integer digit) {
- if (null == digit) {
- digit = 6;
- }
- String code;
- // 生成验证码
- if (digit == 4) {
- code = new Random().nextInt(8999) + 1000 + "";
- } else {
- code = new Random().nextInt(899999) + 100000 + "";
- }
- return code;
- }
- /**
- * 姓名匿名
- */
- public static String anonymity(char[] name) {
- final int size = name.length;
- char[] chars = new char[3];
- if (size == 1 || size == 2) {
- chars[0] = '*';
- chars[1] = ' ';
- chars[2] = name[size - 1];
- } else {
- chars[0] = '*';
- chars[1] = name[size - 2];
- chars[2] = name[size - 1];
- }
- return new String(chars);
- }
- public static String getRealAddressByIP(String ip) {
- if (StrUtil.isBlank(ip)) {
- return "unknown";
- }
- try {
- String rspStr = HttpUtil.get("http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip + "&json=true");
- if (StrUtil.isEmpty(rspStr)) {
- return "unknown";
- }
- JSONObject obj = JSONUtil.parseObj(rspStr);
- String region = obj.getStr("pro");
- String city = obj.getStr("city");
- return String.format("%s %s", region, city);
- } catch (Exception e) {
- }
- return "unknown";
- }
- /**
- * 获取账号登录验证码
- */
- public static String getVerifyCodePattern(String code, Integer length) {
- if (StrUtil.isNotEmpty(code)) {
- Pattern pattern = Pattern.compile(String.format("\\d{%s,}", length));
- Matcher matcher = pattern.matcher(code);
- while (matcher.find()) {
- return matcher.group();
- }
- }
- return StrUtil.EMPTY;
- }
- }
|