• 常用工具类commons-lang3的学习使用


    apache提供的众多commons工具包,号称Java第二API,而common里面lang3包更是被我们使用得最多的。因此本文主要详细讲解lang3包里面几乎每个类的使用,希望以后大家使用此工具包。

    Apache Commons Lang3 Maven 依赖:

    1. <dependency>
    2.   <groupId>org.apache.commonsgroupId>
    3.   <artifactId>commons-lang3artifactId>
    4.   <version>3.9version>
    5. dependency>

    字符串的处理类(StringUtils)

    1. //缩短到某长度,用...结尾.其实就是(substring(str, 0, max-3) + "...")
    2. //public static String abbreviate(String str,int maxWidth)
    3. StringUtils.abbreviate("abcdefg", 6);// ---"abc..."
    4. //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
    5. StringUtils.appendIfMissing("abc","xyz");//---"abcxyz"
    6. StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz");//---"abcXYZ"
    7. //首字母大小写转换
    8. StringUtils.capitalize("cat");//---"Cat"
    9. StringUtils.uncapitalize("Cat");//---"cat"
    10. //字符串扩充至指定大小且居中(若扩充大小少于原字符大小则返回原字符,若扩充大小为 负数则为0计算 )
    11. StringUtils.center("abcd", 2);//--- "abcd"
    12. StringUtils.center("ab", -1);//--- "ab"
    13. StringUtils.center("ab", 4);//---" ab "
    14. StringUtils.center("a", 4, "yz");//---"yayz"
    15. StringUtils.center("abc", 7, "");//---" abc "
    16. //去除字符串中的"\n", "\r", or "\r\n"
    17. StringUtils.chomp("abc\r\n");//---"abc"
    18. //判断一字符串是否包含另一字符串
    19. StringUtils.contains("abc", "z");//---false
    20. StringUtils.containsIgnoreCase("abc", "A");//---true
    21. //统计一字符串在另一字符串中出现次数
    22. StringUtils.countMatches("abba", "a");//---2
    23. //删除字符串中的梭有空格
    24. StringUtils.deleteWhitespace(" ab c ");//---"abc"
    25. //比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
    26. StringUtils.difference("abcde", "abxyz");//---"xyz"
    27. //检查字符串结尾后缀是否匹配
    28. StringUtils.endsWith("abcdef", "def");//---true
    29. StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
    30. StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
    31. //检查起始字符串是否匹配
    32. StringUtils.startsWith("abcdef", "abc");//---true
    33. StringUtils.startsWithIgnoreCase("ABCDEF", "abc");//---true
    34. StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
    35. //判断两字符串是否相同
    36. StringUtils.equals("abc", "abc");//---true
    37. StringUtils.equalsIgnoreCase("abc", "ABC");//---true
    38. //比较字符串数组内的所有元素的字符序列,起始一致则返回一致的字符串,若无则返回""
    39. StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"});//---"ab"
    40. //正向查找字符在字符串中第一次出现的位置
    41. StringUtils.indexOf("aabaabaa", "b");//---2
    42. StringUtils.indexOf("aabaabaa", "b", 3);//---5(从角标3后查找)
    43. StringUtils.ordinalIndexOf("aabaabaa", "a", 3);//---1(查找第n次出现的位置)
    44. //反向查找字符串第一次出现的位置
    45. StringUtils.lastIndexOf("aabaabaa", ‘b‘);//---5
    46. StringUtils.lastIndexOf("aabaabaa", ‘b‘, 4);//---2
    47. StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2);//---1
    48. //判断字符串大写、小写
    49. StringUtils.isAllUpperCase("ABC");//---true
    50. StringUtils.isAllLowerCase("abC");//---false
    51. //判断是否为空(注:isBlank与isEmpty 区别)
    52. StringUtils.isBlank(null);StringUtils.isBlank("");StringUtils.isBlank(" ");//---true
    53. StringUtils.isNoneBlank(" ", "bar");//---false
    54. StringUtils.isEmpty(null);StringUtils.isEmpty("");//---true
    55. StringUtils.isEmpty(" ");//---false
    56. StringUtils.isNoneEmpty(" ", "bar");//---true
    57. //判断字符串数字
    58. StringUtils.isNumeric("123");//---false
    59. StringUtils.isNumeric("12 3");//---false (不识别运算符号、小数点、空格……)
    60. StringUtils.isNumericSpace("12 3");//---true
    61. //数组中加入分隔符号
    62. //StringUtils.join([1, 2, 3], ‘;‘);//---"1;2;3"
    63. //大小写转换
    64. StringUtils.upperCase("aBc");//---"ABC"
    65. StringUtils.lowerCase("aBc");//---"abc"
    66. StringUtils.swapCase("The dog has a BONE");//---"tHE DOG HAS A bone"
    67. //替换字符串内容……(replacePattern、replceOnce)
    68. StringUtils.replace("aba", "a", "z");//---"zbz"
    69. StringUtils.overlay("abcdef", "zz", 2, 4);//---"abzzef"(指定区域)
    70. StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
    71. new String[]{"w", "t"});//---"wcte"(多组指定替换ab->w,d->t)
    72. //重复字符
    73. StringUtils.repeat(‘e‘, 3);//---"eee"
    74. //反转字符串
    75. StringUtils.reverse("bat");//---"tab"
    76. //删除某字符
    77. StringUtils.remove("queued", ‘u‘);//---"qeed"
    78. //分割字符串
    79. StringUtils.split("a..b.c", ‘.‘);//---["a", "b", "c"]
    80. StringUtils.split("ab:cd:ef", ":", 2);//---["ab", "cd:ef"]
    81. StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2);//---["ab", "cd-!-ef"]
    82. StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":");//-["ab"," ","cd","ef"]
    83. //去除首尾空格,类似trim……(stripStart、stripEnd、stripAll、stripAccents)
    84. StringUtils.strip(" ab c ");//---"ab c"
    85. StringUtils.stripToNull(null);//---null
    86. StringUtils.stripToEmpty(null);//---""
    87. //截取字符串
    88. StringUtils.substring("abcd", 2);//---"cd"
    89. StringUtils.substring("abcdef", 2, 4);//---"cd"
    90. //left、right从左(右)开始截取n位字符
    91. StringUtils.left("abc", 2);//---"ab"
    92. StringUtils.right("abc", 2);//---"bc"
    93. //从第n位开始截取m位字符 n m
    94. StringUtils.mid("abcdefg", 2, 4);//---"cdef"
    95. StringUtils.substringBefore("abcba", "b");//---"a"
    96. StringUtils.substringBeforeLast("abcba", "b");//---"abc"
    97. StringUtils.substringAfter("abcba", "b");//---"cba"
    98. StringUtils.substringAfterLast("abcba", "b");//---"a"
    99. StringUtils.substringBetween("tagabctag", "tag");//---"abc"
    100. StringUtils.substringBetween("yabczyabcz", "y", "z");//---"abc"

    扩展

    abbreviateMiddle截取指定字符串,中间使用指定字符代替

    1. /**
    2. *
    3. * 截取指定的字符串,中间用指定字符代替。前面字符个数 + 代替字符个数 + 后2个字符 = 截取的字符个 数
    4. * 显示5个长度的字符串,除了最后一个中间用设置的符号代替
    5. * 长度不能小于 2
    6. */
    7. //截取9个字符,中间使用四个*代替
    8. String str2 = StringUtils.abbreviateMiddle("15889009897","****",9);
    9. System.out.println(str2); //158****97
    10. //字符刚好只有10个
    11. String s1 = StringUtils.abbreviateMiddle("1342354534", "**", 10);
    12. System.out.println(s1); //1342354534
    13. //截取5个字符
    14. String s2 = StringUtils.abbreviateMiddle("12345678910", "*", 5);
    15. System.out.println(s2); //12*10
    16. //截取长度不能小于2
    17. String s3 = StringUtils.abbreviateMiddle("12345678910", "*", 2);
    18. System.out.println(s3); //12345678910

    abbreviate截取指定字符串,多的用指定字符代替

    1. //显示6个长的的字符串,多的用三个点代替(...)
    2. StringUtils.abbreviate("abc中文asdfdefg",6);//输出结果:abc...
    3. StringUtils.abbreviate("这是中文哦可以全部显示吗",5);//这是...
    4. StringUtils.abbreviate("abcdefgh",12); //abcdefgh
    5. StringUtils.abbreviate("abcdaldf","**",3); //a**

       contains检测某个字符串中是否包含某个字符

    1.  //检测abcde字符串中是否包含abc字符
    2.   StringUtils.contains("abcde", "abc"); //true

      containsAny检测某个字符串中是否包含字符串1或者字符串2……

    1.         //检测字符串abcdef中是否保护abm或者y
    2.         StringUtils.containsAny("abcdef", "abm", "y"); //false

     检测abcdefg字符串中是否包含 "b"或者"h"或者"N”字符串

         StringUtils.containsAny("abcdefg", "b", "h", "N"); //true

    defaultString将 null空字符串转为 ""空字符串

    1.     //defaultString将 null空字符串转为 ""空字符串
    2.         StringUtils.defaultString(null);// ""
    3.         StringUtils.defaultString("");// ""
    4.         StringUtils.defaultString("bat");//"bat"

    isEmpty和 isNotEmpty判断字符串是否为空

    1. StringUtils.isEmpty(null);//true
    2. StringUtils.isEmpty("");//true
    3. StringUtils.isEmpty(" ");//false
    4. StringUtils.isEmpty("bob");//false
    5. StringUtils.isEmpty("  bob  ");//false
    6. StringUtils.isNotEmpty(null);//false
    7. StringUtils.isNotEmpty("");//false
    8. StringUtils.isNotEmpty(" ");//true
    9. StringUtils.isNotEmpty("bob");//true
    10. StringUtils.isNotEmpty("  bob  ");//true

     注意:isEmpty和 isBlank区别

    isEmpty认为 " "是不等于空的。isBlank认为 " "是等于空的,具有一个前后去掉空格的意思。

    replace替换字符串

    1. 方法:public static String replace(final String text, final String searchString, final String replacement) {}
    2. 将 text字符串中的 searchString字符替换成 replacement
    3. StringUtils.replace(null,,);//null
    4. StringUtils.replace("",,);//""
    5. StringUtils.replace("any",null,);//"any"
    6. StringUtils.replace("any",,null);//"any"
    7. StringUtils.replace("any","",);//"any"
    8. StringUtils.replace("aba","a",null);//"aba"
    9. StringUtils.replace("aba","a","");//"b"
    10. StringUtils.replace("aba","a","z");//"zbz"

    随机数生成类(RandomUtils)

    1. //返回一个随机布尔值
    2. boolean b = RandomUtils.nextBoolean();
    3. System.out.println(b);
    4. //随机生成10个长的的byte数组
    5. byte[] bytes = RandomUtils.nextBytes(10);
    6. //打印字节数组
    7. System.out.println(Arrays.toString(bytes));
    8. //输出结果:[105, 86, 28, 57, -113, 13, -30, -83, -96, 126]
    9. for (int i = 0; i < 20; i++) {
    10. //指定范围随机数1-5 不包括5
    11. int rand = RandomUtils.nextInt(1, 5);
    12. System.out.print(rand+" ");
    13. }
    14. 输出结果:4 3 2 2 2 2 4 1 3 3 3 2 3 4 4 2 2 1 3 2
    15. //1-4.99999之间的随机数,nextLong,nextFloat
    16. double v = RandomUtils.nextDouble(1, 5); //3.6664777207393597
    17. System.out.println(v);

    随机数生成类(RandomStringUtils)

    1. //随机生成n位数数字
    2. RandomStringUtils.randomNumeric(n);
    3. //在指定字符串中生成长度为n的随机字符串
    4. RandomStringUtils.random(n, "abcdefghijk");
    5. //指定从字符或数字中生成随机字符串
    6. System.out.println(RandomStringUtils.random(n, true, false));
    7. System.out.println(RandomStringUtils.random(n, false, true));

    数字类NumberUtils

    1. //从数组中选出最大值
    2. NumberUtils.max(new int[] { 1, 2, 3, 4 });//---4
    3. //判断字符串是否全是整数
    4. NumberUtils.isDigits("153.4");//--false
    5. //判断字符串是否是有效数字
    6. NumberUtils.isNumber("0321.1");//---false

    数组类 ArrayUtils

    1. //创建数组
    2. String[] array = ArrayUtils.toArray("1", "2");
    3. //判断两个数据是否相等,如果内容相同, 顺序相同 则返回 true
    4. ArrayUtils.isEquals(arr1,arr2);
    5. //判断数组中是否包含某一对象
    6. ArrayUtils.contains(arr, "33");
    7. //二维数组转换成MAP
    8. Map map = ArrayUtils.toMap(new String[][] {
    9. { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } });

    日期类DateUtils

    1. //日期加n天
    2. DateUtils.addDays(new Date(), n);
    3. //判断是否同一天
    4. DateUtils.isSameDay(date1, date2);
    5. //字符串时间转换为Date
    6. DateUtils.parseDate(str, parsePatterns);

    附: StringUtile

    1. /*
    2. * Copyright 2015-2017 the original author or authors.
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package com.funtl.leesite.common.utils;
    17. import java.io.UnsupportedEncodingException;
    18. import java.util.ArrayList;
    19. import java.util.List;
    20. import java.util.Locale;
    21. import java.util.regex.Matcher;
    22. import java.util.regex.Pattern;
    23. import javax.servlet.http.HttpServletRequest;
    24. import com.google.common.collect.Lists;
    25. import org.apache.commons.lang3.StringEscapeUtils;
    26. import org.springframework.web.context.request.RequestContextHolder;
    27. import org.springframework.web.context.request.ServletRequestAttributes;
    28. import org.springframework.web.servlet.LocaleResolver;
    29. /**
    30. * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
    31. *
    32. * @author Lusifer
    33. * @version 2013-05-22
    34. */
    35. public class StringUtils extends org.apache.commons.lang3.StringUtils {
    36. private static final char SEPARATOR = '_';
    37. private static final String CHARSET_NAME = "UTF-8";
    38. /**
    39. * 转换为字节数组
    40. *
    41. * @param str
    42. * @return
    43. */
    44. public static byte[] getBytes(String str) {
    45. if (str != null) {
    46. try {
    47. return str.getBytes(CHARSET_NAME);
    48. } catch (UnsupportedEncodingException e) {
    49. return null;
    50. }
    51. } else {
    52. return null;
    53. }
    54. }
    55. /**
    56. * 转换为字节数组
    57. *
    58. * @param str
    59. * @return
    60. */
    61. public static String toString(byte[] bytes) {
    62. try {
    63. return new String(bytes, CHARSET_NAME);
    64. } catch (UnsupportedEncodingException e) {
    65. return EMPTY;
    66. }
    67. }
    68. /**
    69. * 是否包含字符串
    70. *
    71. * @param str 验证字符串
    72. * @param strs 字符串组
    73. * @return 包含返回true
    74. */
    75. public static boolean inString(String str, String... strs) {
    76. if (str != null) {
    77. for (String s : strs) {
    78. if (str.equals(trim(s))) {
    79. return true;
    80. }
    81. }
    82. }
    83. return false;
    84. }
    85. /**
    86. * 替换掉HTML标签方法
    87. */
    88. public static String replaceHtml(String html) {
    89. if (isBlank(html)) {
    90. return "";
    91. }
    92. String regEx = "<.+?>";
    93. Pattern p = Pattern.compile(regEx);
    94. Matcher m = p.matcher(html);
    95. String s = m.replaceAll("");
    96. return s;
    97. }
    98. /**
    99. * 替换为手机识别的HTML,去掉样式及属性,保留回车。
    100. *
    101. * @param html
    102. * @return
    103. */
    104. public static String replaceMobileHtml(String html) {
    105. if (html == null) {
    106. return "";
    107. }
    108. return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>");
    109. }
    110. /**
    111. * 替换为手机识别的HTML,去掉样式及属性,保留回车。
    112. *
    113. * @param txt
    114. * @return
    115. */
    116. public static String toHtml(String txt) {
    117. if (txt == null) {
    118. return "";
    119. }
    120. return replace(replace(Encodes.escapeHtml(txt), "\n", "
      "
      ), "\t", "    ");
    121. }
    122. /**
    123. * 缩略字符串(不区分中英文字符)
    124. *
    125. * @param str 目标字符串
    126. * @param length 截取长度
    127. * @return
    128. */
    129. public static String abbr(String str, int length) {
    130. if (str == null) {
    131. return "";
    132. }
    133. try {
    134. StringBuilder sb = new StringBuilder();
    135. int currentLength = 0;
    136. for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
    137. currentLength += String.valueOf(c).getBytes("GBK").length;
    138. if (currentLength <= length - 3) {
    139. sb.append(c);
    140. } else {
    141. sb.append("...");
    142. break;
    143. }
    144. }
    145. return sb.toString();
    146. } catch (UnsupportedEncodingException e) {
    147. e.printStackTrace();
    148. }
    149. return "";
    150. }
    151. public static String abbr2(String param, int length) {
    152. if (param == null) {
    153. return "";
    154. }
    155. StringBuffer result = new StringBuffer();
    156. int n = 0;
    157. char temp;
    158. boolean isCode = false; // 是不是HTML代码
    159. boolean isHTML = false; // 是不是HTML特殊字符,如 
    160. for (int i = 0; i < param.length(); i++) {
    161. temp = param.charAt(i);
    162. if (temp == '<') {
    163. isCode = true;
    164. } else if (temp == '&') {
    165. isHTML = true;
    166. } else if (temp == '>' && isCode) {
    167. n = n - 1;
    168. isCode = false;
    169. } else if (temp == ';' && isHTML) {
    170. isHTML = false;
    171. }
    172. try {
    173. if (!isCode && !isHTML) {
    174. n += String.valueOf(temp).getBytes("GBK").length;
    175. }
    176. } catch (UnsupportedEncodingException e) {
    177. e.printStackTrace();
    178. }
    179. if (n <= length - 3) {
    180. result.append(temp);
    181. } else {
    182. result.append("...");
    183. break;
    184. }
    185. }
    186. // 取出截取字符串中的HTML标记
    187. String temp_result = result.toString().replaceAll("(>)[^<>]*(, "$1$2");
    188. // 去掉不需要结素标记的HTML标记
    189. temp_result = temp_result.replaceAll("]*/?>", "");
    190. // 去掉成对的HTML标记
    191. temp_result = temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)", "$2");
    192. // 用正则表达式取出标记
    193. Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
    194. Matcher m = p.matcher(temp_result);
    195. List endHTML = Lists.newArrayList();
    196. while (m.find()) {
    197. endHTML.add(m.group(1));
    198. }
    199. // 补全不成对的HTML标记
    200. for (int i = endHTML.size() - 1; i >= 0; i--) {
    201. result.append(");
    202. result.append(endHTML.get(i));
    203. result.append(">");
    204. }
    205. return result.toString();
    206. }
    207. /**
    208. * 转换为Double类型
    209. */
    210. public static Double toDouble(Object val) {
    211. if (val == null) {
    212. return 0D;
    213. }
    214. try {
    215. return Double.valueOf(trim(val.toString()));
    216. } catch (Exception e) {
    217. return 0D;
    218. }
    219. }
    220. /**
    221. * 转换为Float类型
    222. */
    223. public static Float toFloat(Object val) {
    224. return toDouble(val).floatValue();
    225. }
    226. /**
    227. * 转换为Long类型
    228. */
    229. public static Long toLong(Object val) {
    230. return toDouble(val).longValue();
    231. }
    232. /**
    233. * 转换为Integer类型
    234. */
    235. public static Integer toInteger(Object val) {
    236. return toLong(val).intValue();
    237. }
    238. /**
    239. * 获得i18n字符串
    240. */
    241. public static String getMessage(String code, Object[] args) {
    242. LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
    243. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    244. Locale localLocale = localLocaleResolver.resolveLocale(request);
    245. return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
    246. }
    247. /**
    248. * 获得用户远程地址
    249. */
    250. public static String getRemoteAddr(HttpServletRequest request) {
    251. String remoteAddr = request.getHeader("X-Real-IP");
    252. if (isNotBlank(remoteAddr)) {
    253. remoteAddr = request.getHeader("X-Forwarded-For");
    254. } else if (isNotBlank(remoteAddr)) {
    255. remoteAddr = request.getHeader("Proxy-Client-IP");
    256. } else if (isNotBlank(remoteAddr)) {
    257. remoteAddr = request.getHeader("WL-Proxy-Client-IP");
    258. }
    259. return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
    260. }
    261. /**
    262. * 驼峰命名法工具
    263. *
    264. * @return toCamelCase("hello_world") == "helloWorld"
    265. * toCapitalizeCamelCase("hello_world") == "HelloWorld"
    266. * toUnderScoreCase("helloWorld") = "hello_world"
    267. */
    268. public static String toCamelCase(String s) {
    269. if (s == null) {
    270. return null;
    271. }
    272. s = s.toLowerCase();
    273. StringBuilder sb = new StringBuilder(s.length());
    274. boolean upperCase = false;
    275. for (int i = 0; i < s.length(); i++) {
    276. char c = s.charAt(i);
    277. if (c == SEPARATOR) {
    278. upperCase = true;
    279. } else if (upperCase) {
    280. sb.append(Character.toUpperCase(c));
    281. upperCase = false;
    282. } else {
    283. sb.append(c);
    284. }
    285. }
    286. return sb.toString();
    287. }
    288. /**
    289. * 驼峰命名法工具
    290. *
    291. * @return toCamelCase("hello_world") == "helloWorld"
    292. * toCapitalizeCamelCase("hello_world") == "HelloWorld"
    293. * toUnderScoreCase("helloWorld") = "hello_world"
    294. */
    295. public static String toCapitalizeCamelCase(String s) {
    296. if (s == null) {
    297. return null;
    298. }
    299. s = toCamelCase(s);
    300. return s.substring(0, 1).toUpperCase() + s.substring(1);
    301. }
    302. /**
    303. * 驼峰命名法工具
    304. *
    305. * @return toCamelCase("hello_world") == "helloWorld"
    306. * toCapitalizeCamelCase("hello_world") == "HelloWorld"
    307. * toUnderScoreCase("helloWorld") = "hello_world"
    308. */
    309. public static String toUnderScoreCase(String s) {
    310. if (s == null) {
    311. return null;
    312. }
    313. StringBuilder sb = new StringBuilder();
    314. boolean upperCase = false;
    315. for (int i = 0; i < s.length(); i++) {
    316. char c = s.charAt(i);
    317. boolean nextUpperCase = true;
    318. if (i < (s.length() - 1)) {
    319. nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
    320. }
    321. if ((i > 0) && Character.isUpperCase(c)) {
    322. if (!upperCase || !nextUpperCase) {
    323. sb.append(SEPARATOR);
    324. }
    325. upperCase = true;
    326. } else {
    327. upperCase = false;
    328. }
    329. sb.append(Character.toLowerCase(c));
    330. }
    331. return sb.toString();
    332. }
    333. /**
    334. * 如果不为空,则设置值
    335. *
    336. * @param target
    337. * @param source
    338. */
    339. public static void setValueIfNotBlank(String target, String source) {
    340. if (isNotBlank(source)) {
    341. target = source;
    342. }
    343. }
    344. /**
    345. * 转换为JS获取对象值,生成三目运算返回结果
    346. *
    347. * @param objectString 对象串
    348. * 例如:row.user.id
    349. * 返回:!row?'':!row.user?'':!row.user.id?'':row.user.id
    350. */
    351. public static String jsGetVal(String objectString) {
    352. StringBuilder result = new StringBuilder();
    353. StringBuilder val = new StringBuilder();
    354. String[] vals = split(objectString, ".");
    355. for (int i = 0; i < vals.length; i++) {
    356. val.append("." + vals[i]);
    357. result.append("!" + (val.substring(1)) + "?'':");
    358. }
    359. result.append(val.substring(1));
    360. return result.toString();
    361. }
    362. /**
    363. * 通过正则表达式获取内容
    364. *
    365. * @param regex 正则表达式
    366. * @param from 原字符串
    367. * @return
    368. */
    369. public static String[] regex(String regex, String from) {
    370. Pattern pattern = Pattern.compile(regex);
    371. Matcher matcher = pattern.matcher(from);
    372. List results = new ArrayList();
    373. while (matcher.find()) {
    374. for (int i = 0; i < matcher.groupCount(); i++) {
    375. results.add(matcher.group(i + 1));
    376. }
    377. }
    378. return results.toArray(new String[]{});
    379. }
    380. }

  • 相关阅读:
    在ubuntu上用QT写一个简单的C++小游戏(附源码)
    各种获取JVM DUMP的方法
    leetcode 11.盛最多水的容器
    紫光展锐6nm国产5G处理器T820_国产手机芯片5G方案
    R语言dplyr包select函数筛选dataframe数据中以指定字符串开头的数据列(变量)
    【群智能算法改进】一种改进的鹈鹕优化算法 IPOA算法[1]【Matlab代码#57】
    树状数组略解
    GBase 8d的特性-高性能
    【TB作品】基于MSP430G2553单片机的超声波测距与报警系统,原理图,PCB
    天气这么好,都外出了。顺便了解一下漏桶算法
  • 原文地址:https://blog.csdn.net/weixin_53998054/article/details/126125304