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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| package com.example.demo.utils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils;
import java.text.ParseException; import java.util.Date; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class CommonStringUtils {
private static final String[] IS_DATE_PATTERNS = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };
public static final Pattern IS_NUM_PATTERN = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
public static final Pattern MESSY_CODE_PATTERN = Pattern.compile("\\s*|\t*|\r*|\n*");
public static Date parseDate(String string) { if (string == null) { return null; } try { return DateUtils.parseDate(string, IS_DATE_PATTERNS); } catch (ParseException e) { return null; } }
public static boolean isNumeric(String str) { return IS_NUM_PATTERN.matcher(str).matches(); }
public static String getRandom(int len) { Random r = new Random(); StringBuilder rs = new StringBuilder(); for (int i = 0; i < len; i++) { rs.append(r.nextInt(10)); } return rs.toString(); }
public static String getCharacters(int len, String character) { StringBuilder rs = new StringBuilder(); for (int i = 0; i < len; i++) { rs.append(character); } return rs.toString(); }
public static String moveToLeft(String str, int position) { String str1 = str.substring(position); String str2 = str.substring(0, position); return str1 + str2; }
public static String moveToRight(String str, int position) { String str1 = str.substring(str.length() - position); String str2 = str.substring(0, str.length() - position); return str1 + str2; }
public static boolean isMessyCode(String strName) { try { Matcher m = MESSY_CODE_PATTERN.matcher(strName); String after = m.replaceAll(""); String temp = after.replaceAll("\\p{P}", ""); char[] ch = temp.trim().toCharArray();
for (char c : ch) { if (!Character.isLetterOrDigit(c)) { String str = "" + c; if (!str.matches("[\u4e00-\u9fa5]+")) { return true; } } } } catch (Exception e) { e.printStackTrace(); }
return false; }
public static String keywordReplacement(String rawData, String keyword, String replacementString) throws Exception { StringBuilder stringBuilder = new StringBuilder(rawData);
Matcher m = Pattern.compile(keyword).matcher(stringBuilder); StringBuffer appendReplacement = new StringBuffer(); while (m.find()) { m.appendReplacement(appendReplacement, replacementString); } if (StringUtils.isEmpty(appendReplacement)) { throw new Exception("原始数据里没有匹配到关键字"); } m.appendTail(appendReplacement);
return appendReplacement.toString(); } }
|