1. package com.isyscore.robot.core.util;
    2. import java.util.LinkedList;
    3. import java.util.List;
    4. import java.util.StringTokenizer;
    5. import java.util.regex.Matcher;
    6. import java.util.regex.Pattern;
    7. /**
    8. * @author shizi
    9. * @since 2019/12/3 11:48 上午
    10. */
    11. public class Strings {
    12. /**
    13. * 返回指定字符串中匹配执行正则表达式的子字符串序列。
    14. *
    15. * @param s 待处理的字符串。
    16. * @param re 需要查找的正则表达式
    17. * @return 返回 s 中匹配上 re 的字符串序列。
    18. */
    19. public static List<String> reSeq(String s, String re) {
    20. List<String> list = new LinkedList<>();
    21. Matcher matcher = Pattern.compile(re).matcher(s);
    22. while (matcher.find()) {
    23. list.add(matcher.group());
    24. }
    25. return list;
    26. }
    27. /**
    28. * 返回指定字符串中指定字符出现的次数。
    29. *
    30. * @param s 待处理的字符串。
    31. * @param c 待查找的字符。
    32. * @return 返回 s 中出现 c 的次数。
    33. */
    34. public static int countOccurrencesOf(String s, char c) {
    35. int count = 0;
    36. for (int index = -1; (index = s.indexOf(c, index + 1)) >= 0; count++){}
    37. return count;
    38. }
    39. static String identity(String s) {
    40. return s;
    41. }
    42. /**
    43. * 将驼峰转换成非严格的下划线风格。
    44. *
    45. * 遇到连续的大写字母,仅在第一个大写字母前添加下划线。 例如 UserID 转换成 user_id。
    46. *
    47. * @param s 待转换字符串
    48. * @return 如果输入是null,则返回null,否则返回转换后的字符串
    49. */
    50. public static String toUnderScore(String s) {
    51. return s == null ? null : s.replaceAll("(?<=[^A-Z])[A-Z]", "_$0").toLowerCase();
    52. }
    53. /**
    54. * 将驼峰转换成严格的下划线风格。
    55. *
    56. * 遇到连续的大写字母,在每个大写字母前添加下划线。 例如 UserID 转换成 user_i_d。
    57. *
    58. * @param s 待转换字符串
    59. * @return 如果输入是null,则返回null,否则返回转换后的字符串
    60. */
    61. public static String toUnderScoreStrict(String s) {
    62. return s == null ? null : s.replaceAll("\\B[A-Z]", "_$0").toLowerCase();
    63. }
    64. private final static Pattern patternUnderScore = Pattern.compile("(?<=_)[a-zA-Z]");
    65. private final static Pattern patternUnderScoreAll = Pattern.compile("(?<=^|_)[a-zA-Z]");
    66. private static String toCamelCase(Pattern pattern, String s) {
    67. if (s == null) {
    68. return null;
    69. }
    70. StringBuffer name = new StringBuffer();
    71. Matcher m = pattern.matcher(s);
    72. while (m.find()) {
    73. m.appendReplacement(name, m.group().toUpperCase());
    74. }
    75. m.appendTail(name);
    76. return name.toString().replaceAll("_+", "");
    77. }
    78. /**
    79. * 非严格的驼峰风格,仅将下划线后的字母大写化,其他字母保持不变。
    80. *
    81. * 例如 user_iD 转换成改成 userID。
    82. *
    83. * @param s 待转换字符串
    84. * @return 如果输入是null,则返回null,否则返回转换后的字符串
    85. */
    86. public static String toCamelCase(String s) {
    87. return toCamelCase(patternUnderScore, s);
    88. }
    89. /**
    90. * 严格的驼峰风格,除了将下划线后的字母大写化,其他字母全部小写。
    91. *
    92. * 例如 user_iD 转换成改成 userId。
    93. *
    94. * @param s 待转换字符串
    95. * @return 如果输入是null,则返回null,否则返回转换后的字符串
    96. */
    97. public static String toCamelCaseStrict(String s) {
    98. return toCamelCase(patternUnderScore, s == null ? null : s.toLowerCase());
    99. }
    100. /**
    101. * 非严格的驼峰风格,仅将开头的或下划线后的字母大写化,其他字母保持不变。
    102. *
    103. * 例如 user_iD 转换成改成 UserID。
    104. *
    105. * @param s 待转换字符串
    106. * @return 如果输入是null,则返回null,否则返回转换后的字符串
    107. */
    108. public static String toCamelCaseAll(String s) {
    109. return toCamelCase(patternUnderScoreAll, s);
    110. }
    111. /**
    112. * 严格的驼峰风格,除了将开头或下划线后的字母大写化,其他字母全部小写。
    113. *
    114. * 例如 user_iD 转换成改成 UserId。
    115. *
    116. * @param s 待转换字符串
    117. * @return 如果输入是null,则返回null,否则返回转换后的字符串
    118. */
    119. public static String toCamelCaseAllStrict(String s) {
    120. return toCamelCase(patternUnderScoreAll, s == null ? null : s.toLowerCase());
    121. }
    122. /**
    123. * 转成小写字母。
    124. *
    125. * @param s 待转换字符串
    126. * @return 如果输入是null,这返回null,否则返回转换后的字符串
    127. */
    128. public static String toLowerCase(String s) {
    129. return s != null? s.toLowerCase(): null;
    130. }
    131. /**
    132. * 仅保留转换后的小写字母。
    133. *
    134. * @param s 待转换字符串
    135. * @return 如果输入是null,这返回null,否则返回转换后的字符串
    136. */
    137. public static String toLowerCaseOnly(String s) {
    138. return s != null? s.toLowerCase().replaceAll("[^0-9a-z]+", ""): null;
    139. }
    140. /**
    141. * 转成大写字母。
    142. *
    143. * @param s 待转换字符串
    144. * @return 如果输入是null,这返回null,否则返回转换后的字符串
    145. */
    146. public static String toUpperCase(String s) {
    147. return s != null? s.toUpperCase(): null;
    148. }
    149. /**
    150. * 仅保留转换后的大写字母。
    151. *
    152. * @param s 待转换字符串
    153. * @return 如果输入是null,这返回null,否则返回转换后的字符串
    154. */
    155. public static String toUpperCaseOnly(String s) {
    156. return s != null? s.toUpperCase().replaceAll("[^0-9A-Z]", ""): null;
    157. }
    158. /**
    159. * 使用StringTokenizer分割字符串,避免正则
    160. * @param source 待分割的字符串
    161. * @param delim 分割符
    162. * @return 按照分割符分割好的字符串数组
    163. */
    164. public static String[] quickSplit(String source, String delim) {
    165. if (null == source || null == delim) {
    166. throw new NullPointerException();
    167. }
    168. StringTokenizer tokenizer = new StringTokenizer(source, delim);
    169. String[] result = new String[tokenizer.countTokens()];
    170. for (int i = 0; i < result.length; i++) {
    171. result[i] = tokenizer.nextToken();
    172. }
    173. return result;
    174. }
    175. /**
    176. * 去掉指定字符串的开头的指定字符
    177. * @param str 原始字符串
    178. * @param trim 要删除的字符串
    179. * @return lk_user_name -> user_name
    180. */
    181. public static String trimStartStr(String str, String trim) {
    182. // null或者空字符串的时候不处理
    183. if (str == null || str.length() == 0 || trim == null || trim.length() == 0) {
    184. return str;
    185. }
    186. // 要删除的字符串结束位置
    187. int end;
    188. Pattern pattern = Pattern.compile("[" + trim + "]*+", Pattern.CASE_INSENSITIVE);
    189. Matcher matcher = pattern.matcher(str);
    190. if (matcher.lookingAt()) {
    191. end = matcher.end();
    192. str = str.substring(end);
    193. }
    194. return str;
    195. }
    196. }