StringUtils.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package com.ydtech.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Base64;
  4. import java.io.PrintWriter;
  5. import java.io.StringWriter;
  6. import java.nio.charset.StandardCharsets;
  7. import java.util.Arrays;
  8. import java.util.Map;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. /**
  13. * 字符操作常用方法集
  14. * Created by zhouhao on 16-6-6.
  15. */
  16. public class StringUtils extends org.apache.commons.lang3.StringUtils {
  17. /**
  18. * 编译后的正则表达式缓存
  19. */
  20. private static final Map<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>();
  21. /**
  22. * 编译一个正则表达式,并且进行缓存,如果换成已存在则使用缓存
  23. *
  24. * @param regex 表达式
  25. * @return 编译后的Pattern
  26. */
  27. public static final Pattern compileRegex(String regex) {
  28. Pattern pattern = PATTERN_CACHE.get(regex);
  29. if (pattern == null) {
  30. pattern = Pattern.compile(regex);
  31. PATTERN_CACHE.put(regex, pattern);
  32. }
  33. return pattern;
  34. }
  35. /**
  36. * 将字符串的第一位转为小写
  37. *
  38. * @param str 需要转换的字符串
  39. * @return 转换后的字符串
  40. */
  41. public static String toLowerCaseFirstOne(String str) {
  42. if (Character.isLowerCase(str.charAt(0)))
  43. return str;
  44. else {
  45. char[] chars = str.toCharArray();
  46. chars[0] = Character.toLowerCase(chars[0]);
  47. return new String(chars);
  48. }
  49. }
  50. /**
  51. * 将字符串的第一位转为大写
  52. *
  53. * @param str 需要转换的字符串
  54. * @return 转换后的字符串
  55. */
  56. public static String toUpperCaseFirstOne(String str) {
  57. if (Character.isUpperCase(str.charAt(0)))
  58. return str;
  59. else {
  60. char[] chars = str.toCharArray();
  61. chars[0] = Character.toUpperCase(chars[0]);
  62. return new String(chars);
  63. }
  64. }
  65. /**
  66. * 下划线命名转为驼峰命名
  67. *
  68. * @param str 下划线命名格式
  69. * @return 驼峰命名格式
  70. */
  71. public static final String underScoreCase2CamelCase(String str) {
  72. if (!str.contains("_")) return str;
  73. StringBuilder sb = new StringBuilder();
  74. char[] chars = str.toCharArray();
  75. boolean hitUnderScore = false;
  76. sb.append(chars[0]);
  77. for (int i = 1; i < chars.length; i++) {
  78. char c = chars[i];
  79. if (c == '_') {
  80. hitUnderScore = true;
  81. } else {
  82. if (hitUnderScore) {
  83. sb.append(Character.toUpperCase(c));
  84. hitUnderScore = false;
  85. } else {
  86. sb.append(c);
  87. }
  88. }
  89. }
  90. return sb.toString();
  91. }
  92. /**
  93. * 驼峰命名法转为下划线命名
  94. *
  95. * @param str 驼峰命名格式
  96. * @return 下划线命名格式
  97. */
  98. public static final String camelCase2UnderScoreCase(String str) {
  99. StringBuilder sb = new StringBuilder();
  100. char[] chars = str.toCharArray();
  101. for (int i = 0; i < chars.length; i++) {
  102. char c = chars[i];
  103. if (Character.isUpperCase(c)) {
  104. sb.append("_").append(Character.toLowerCase(c));
  105. } else {
  106. sb.append(c);
  107. }
  108. }
  109. return sb.toString();
  110. }
  111. /**
  112. * 将异常栈信息转为字符串
  113. *
  114. * @param e 字符串
  115. * @return 异常栈
  116. */
  117. public static String throwable2String(Throwable e) {
  118. StringWriter writer = new StringWriter();
  119. e.printStackTrace(new PrintWriter(writer));
  120. return writer.toString();
  121. }
  122. /**
  123. * 字符串连接,将参数列表拼接为一个字符串
  124. *
  125. * @param more 追加
  126. * @return 返回拼接后的字符串
  127. */
  128. public static String concat(Object... more) {
  129. return concatSpiltWith("", more);
  130. }
  131. public static String concatSpiltWith(String split, Object... more) {
  132. StringBuilder buf = new StringBuilder();
  133. for (int i = 0; i < more.length; i++) {
  134. if (i != 0) buf.append(split);
  135. buf.append(more[i]);
  136. }
  137. return buf.toString();
  138. }
  139. /**
  140. * 将字符串转移为ASCII码
  141. *
  142. * @param str 字符串
  143. * @return 字符串ASCII码
  144. */
  145. public static String toASCII(String str) {
  146. StringBuffer strBuf = new StringBuffer();
  147. byte[] bGBK = str.getBytes();
  148. for (int i = 0; i < bGBK.length; i++) {
  149. strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
  150. }
  151. return strBuf.toString();
  152. }
  153. public static String toUnicode(String str) {
  154. StringBuffer strBuf = new StringBuffer();
  155. char[] chars = str.toCharArray();
  156. for (int i = 0; i < chars.length; i++) {
  157. strBuf.append("\\u").append(Integer.toHexString(chars[i]));
  158. }
  159. return strBuf.toString();
  160. }
  161. public static String toUnicodeString(char[] chars) {
  162. StringBuffer strBuf = new StringBuffer();
  163. for (int i = 0; i < chars.length; i++) {
  164. strBuf.append("\\u").append(Integer.toHexString(chars[i]));
  165. }
  166. return strBuf.toString();
  167. }
  168. static final char CN_CHAR_START = '\u4e00';
  169. static final char CN_CHAR_END = '\u9fa5';
  170. /**
  171. * 是否包含中文字符
  172. *
  173. * @param str 要判断的字符串
  174. * @return 是否包含中文字符
  175. */
  176. public static boolean containsChineseChar(String str) {
  177. char[] chars = str.toCharArray();
  178. for (int i = 0; i < chars.length; i++) {
  179. if (chars[i] >= CN_CHAR_START && chars[i] <= CN_CHAR_END) return true;
  180. }
  181. return false;
  182. }
  183. /**
  184. * 对象是否为无效值
  185. *
  186. * @param obj 要判断的对象
  187. * @return 是否为有效值(不为null 和 "" 字符串)
  188. */
  189. public static boolean isNullOrEmpty(Object obj) {
  190. return obj == null || "".equals(obj.toString().trim());
  191. }
  192. /**
  193. * 参数是否是有效数字 (整数或者小数)
  194. *
  195. * @param obj 参数(对象将被调用string()转为字符串类型)
  196. * @return 是否是数字
  197. */
  198. public static boolean isNumber(Object obj) {
  199. if (obj instanceof Number) return true;
  200. return isInt(obj) || isDouble(obj);
  201. }
  202. public static String matcherFirst(String patternStr, String text) {
  203. Pattern pattern = compileRegex(patternStr);
  204. Matcher matcher = pattern.matcher(text);
  205. String group = null;
  206. if (matcher.find()) {
  207. group = matcher.group();
  208. }
  209. return group;
  210. }
  211. /**
  212. * 参数是否是有效整数
  213. *
  214. * @param obj 参数(对象将被调用string()转为字符串类型)
  215. * @return 是否是整数
  216. */
  217. public static boolean isInt(Object obj) {
  218. if (isNullOrEmpty(obj))
  219. return false;
  220. if (obj instanceof Integer)
  221. return true;
  222. return obj.toString().matches("[-+]?\\d+");
  223. }
  224. /**
  225. * 字符串参数是否是double
  226. *
  227. * @param obj 参数(对象将被调用string()转为字符串类型)
  228. * @return 是否是double
  229. */
  230. public static boolean isDouble(Object obj) {
  231. if (isNullOrEmpty(obj))
  232. return false;
  233. if (obj instanceof Double || obj instanceof Float)
  234. return true;
  235. return compileRegex("[-+]?\\d+\\.\\d+").matcher(obj.toString()).matches();
  236. }
  237. /**
  238. * 判断一个对象是否为boolean类型,包括字符串中的true和false
  239. *
  240. * @param obj 要判断的对象
  241. * @return 是否是一个boolean类型
  242. */
  243. public static boolean isBoolean(Object obj) {
  244. if (obj instanceof Boolean) return true;
  245. String strVal = String.valueOf(obj);
  246. return "true".equalsIgnoreCase(strVal) || "false".equalsIgnoreCase(strVal);
  247. }
  248. /**
  249. * 对象是否为true
  250. *
  251. * @param obj
  252. * @return
  253. */
  254. public static boolean isTrue(Object obj) {
  255. return "true".equals(String.valueOf(obj));
  256. }
  257. /**
  258. * 判断一个数组里是否包含指定对象
  259. *
  260. * @param arr 对象数组
  261. * @param obj 要判断的对象
  262. * @return 是否包含
  263. */
  264. public static boolean contains(Object arr[], Object... obj) {
  265. if (arr == null || obj == null || arr.length == 0) return false;
  266. return Arrays.asList(arr).containsAll(Arrays.asList(obj));
  267. }
  268. /**
  269. * 将对象转为int值,如果对象无法进行转换,则使用默认值
  270. *
  271. * @param object 要转换的对象
  272. * @param defaultValue 默认值
  273. * @return 转换后的值
  274. */
  275. public static int toInt(Object object, int defaultValue) {
  276. if (object instanceof Number)
  277. return ((Number) object).intValue();
  278. if (isInt(object)) {
  279. return Integer.parseInt(object.toString());
  280. }
  281. if (isDouble(object)) {
  282. return (int) Double.parseDouble(object.toString());
  283. }
  284. return defaultValue;
  285. }
  286. /**
  287. * 将对象转为int值,如果对象不能转为,将返回0
  288. *
  289. * @param object 要转换的对象
  290. * @return 转换后的值
  291. */
  292. public static int toInt(Object object) {
  293. return toInt(object, 0);
  294. }
  295. /**
  296. * 将对象转为long类型,如果对象无法转换,将返回默认值
  297. *
  298. * @param object 要转换的对象
  299. * @param defaultValue 默认值
  300. * @return 转换后的值
  301. */
  302. public static long toLong(Object object, long defaultValue) {
  303. if (object instanceof Number)
  304. return ((Number) object).longValue();
  305. if (isInt(object)) {
  306. return Long.parseLong(object.toString());
  307. }
  308. if (isDouble(object)) {
  309. return (long) Double.parseDouble(object.toString());
  310. }
  311. return defaultValue;
  312. }
  313. /**
  314. * 将对象转为 long值,如果无法转换,则转为0
  315. *
  316. * @param object 要转换的对象
  317. * @return 转换后的值
  318. */
  319. public static long toLong(Object object) {
  320. return toLong(object, 0);
  321. }
  322. /**
  323. * 将对象转为Double,如果对象无法转换,将使用默认值
  324. *
  325. * @param object 要转换的对象
  326. * @param defaultValue 默认值
  327. * @return 转换后的值
  328. */
  329. public static double toDouble(Object object, double defaultValue) {
  330. if (object instanceof Number)
  331. return ((Number) object).doubleValue();
  332. if (isNumber(object)) {
  333. return Double.parseDouble(object.toString());
  334. }
  335. if (null == object) return defaultValue;
  336. return 0;
  337. }
  338. /**
  339. * 将对象转为Double,如果对象无法转换,将使用默认值0
  340. *
  341. * @param object 要转换的对象
  342. * @return 转换后的值
  343. */
  344. public static double toDouble(Object object) {
  345. return toDouble(object, 0);
  346. }
  347. /**
  348. * 分隔字符串,根据正则表达式分隔字符串,只分隔首个,剩下的的不进行分隔,如: 1,2,3,4 将分隔为 ['1','2,3,4']
  349. *
  350. * @param str 要分隔的字符串
  351. * @param regex 分隔表达式
  352. * @return 分隔后的数组
  353. */
  354. public static String[] splitFirst(String str, String regex) {
  355. return str.split(regex, 2);
  356. }
  357. /**
  358. * 将对象转为字符串,如果对象为null,则返回null,而不是"null"
  359. *
  360. * @param object 要转换的对象
  361. * @return 转换后的对象
  362. */
  363. public static String toString(Object object) {
  364. return toString(object, null);
  365. }
  366. /**
  367. * 将对象转为字符串,如果对象为null,则使用默认值
  368. *
  369. * @param object 要转换的对象
  370. * @param defaultValue 默认值
  371. * @return 转换后的字符串
  372. */
  373. public static String toString(Object object, String defaultValue) {
  374. if (object == null) return defaultValue;
  375. return String.valueOf(object);
  376. }
  377. /**
  378. * 将对象转为String后进行分割,如果为对象为空或者空字符,则返回null
  379. *
  380. * @param object 要分隔的对象
  381. * @param regex 分隔规则
  382. * @return 分隔后的对象
  383. */
  384. public static final String[] toStringAndSplit(Object object, String regex) {
  385. if (isNullOrEmpty(object)) return null;
  386. return String.valueOf(object).split(regex);
  387. }
  388. private static boolean isChinese(char c) {
  389. Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
  390. if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
  391. || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  392. || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
  393. || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
  394. || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
  395. || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
  396. return true;
  397. }
  398. return false;
  399. }
  400. public static boolean isMessyCode(String strName) {
  401. Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
  402. Matcher m = p.matcher(strName);
  403. String after = m.replaceAll("");
  404. String temp = after.replaceAll("\\p{P}", "");
  405. char[] ch = temp.trim().toCharArray();
  406. float chLength = 0;
  407. float count = 0;
  408. for (int i = 0; i < ch.length; i++) {
  409. char c = ch[i];
  410. if (!Character.isLetterOrDigit(c)) {
  411. if (!isChinese(c)) {
  412. count = count + 1;
  413. }
  414. chLength++;
  415. }
  416. }
  417. float result = count / chLength;
  418. if (result > 0.4) {
  419. return true;
  420. } else {
  421. return false;
  422. }
  423. }
  424. public static double parseDouble(String s) {
  425. if (StringUtils.isNullOrEmpty(s)) {
  426. return 0;
  427. }
  428. return Double.parseDouble(s);
  429. }
  430. }