学习笔记:Apache commons-lang3


     编者按

在 Maven项目的 pom.xml 加入 commons-lang3 的 dependency;就可以使用 各种 工具类了。
这里 并没有写很多的demo,发现官方API举的 demo 已经很多了,复制代码到 Java编译工具,鼠标移到方法名上 即可看到。

  	<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-lang3</artifactId>
	    <version>3.9</version>
	</dependency>

博主原文连接


     01. StringUtils 工具类

         1.1 装x操作
	/**
	 * 1. public static String deleteWhitespace(final String str)
	 *    删除空格
	 *    
	 * 2. public static String capitalize(final String str)
	 *    使首字母大写, 如果首字母是非字母 那不生效的。
	 * 
     * 3. public static String uncapitalize(final String str)
     *    使首字母小写, 如果首字母是非字母 那不生效的。
     *  
     * 4. public static String swapCase(final String str)
     *    字串串中的小写字母转小写,小写转大写。
     *    
     * 5. public static String defaultString(final String str) 
     *    如果字符串为null 那么返回"",否则返回原字符串

     * 6. public static String defaultString(final String str, final String defaultStr)
     *    如果字符串为null 那么返回 defaultStr,否则返回原字符串
     *
     * 7. public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultStr)    [OK]
     *    如果字符串为 "", " ", null, (多个空格如 "   " 也返回 defaultStr); 那么返回 defaultStr, 否则返回原字符串
     *
     * 8. public static <T extends CharSequence> T defaultIfEmpty(final T str, final T defaultStr)    [OK]
     *    如果字符串为 "", null 那么返回 defaultStr, 否则返回原字符串
     *
     * 9. public static String wrap(final String str, final String wrapWith)
     *    使用字符串 wrapWith 对字符串进行包裹.   StringUtils.wrap("ab", '\'')     = "'ab'"
	 */
	@Test
	public void 新颖操作() {
		String s1 = "a bc ";
		s1 = StringUtils.deleteWhitespace(s1);
		System.out.println("s1 = " + s1); // s1 = abc
		
		String s2 = StringUtils.capitalize("a bc");
		System.out.println("s2 = " + s2); // s2 = A bc
		
		String s3 = StringUtils.swapCase("a b 2 C"); 
		System.out.println("s3 = " + s3); // s3 = A B 2 c
		
		String s4 = StringUtils.defaultString(null).toString();
		String s5 = StringUtils.defaultString("", "");
		String s6 = StringUtils.defaultIfBlank(null, "peove");
		System.out.println("s6 = " + s6);
		String s7 = StringUtils.defaultIfEmpty(null, "Peove");
		System.out.println("s7 = " + s7);
		
		StringUtils.wrap("", 'a');
		String s8 = StringUtils.wrap("陪我长大的愿望", "\'"); // 其实 ' 不用转义, 去掉 \ 也可以。 
		System.out.println("s8 = " + s8); // s8 = '陪我长大的愿望'
	}

附:Java 需要转义的符号

在Java中, 不管是String.split(), 还是正则表达式, 有一些特殊字符需要转义;
这些字符是     ( [ { / ^ - $ ¦ } ] ) ? * + .
转义方法为字符前面加上 “\”, 这样在split、replaceAll时就不会报错了;
不过要注意, String.contains() 方法不需要转义。

         1.2 为空操作
	/** 
	 *      *                 *
	 *     * *               * *
	 *    * * *             * * * 
	 *   * * * *  为空操作  * * * * 
	 *   
	 * 忽然发现 人家方法注解里 写的更清楚... 
	 * oc, 公司用的 老版本 Commons-lang 没注解啊.. 还是2.6版本的, 2011年1月的...
	 * 
	 * 1. StringUtils.isEmpty()   : null / "" 为true;    " " 为false.
	 * 2. StringUtils.isNOtEmpty(): null / "" 为false;   " " 为true.
	 * 3. StringUtils.isBlank()   : null / "" / " " 为true.
	 * 4. StringUtils.isNotBlank(): null / "" / " " 为false.
	 * 
	 * 5. [×] StingUtils.trim()   : StingUtils.trim() 可以兼容null, 不会报NPE, 其他地方和 String.trim() 一样, 
	 *    源码里边就这么处理的: return str == null ? null : str.trim(); ...
	 *    所以, 返回的结果还是 不安全, 还是加if 非空判断, 再用.
	 * 
	 * 6. [×] StringUtils.equalsIgnoreCase(): 判断2个字符串是否相等, 忽略大小写.
	 *    甚至可以不用对 2个参数 进行非空判断, 如果 任何一个为空, 都会返回 false. 
	 *    可以兼容null, 不会报NPE, 其他地方和 String.equalsIgnoreCase() 一样.
	 *    
	 * 7. StringUtils.equalsAny(string, searchStrings..)          : string是否与searchStrings中任意一个字符串相等.
	 * 8. StringUtils.equalsAnyIgnoreCase(string, searchStrings..): 同上, 忽略大小写.
	 */
	@Test
	public void 为空操作() {
		
		String s1 = null;
		String s2 = "";
		String s3 = " ";
		
		System.out.println(StringUtils.isEmpty(s1)); // true
		System.out.println(StringUtils.isEmpty(s2)); // true
		System.out.println(StringUtils.isEmpty(s3)); // false
		
		System.out.println(StringUtils.isNotEmpty(s1)); // false 
		System.out.println(StringUtils.isNotEmpty(s2)); // false
		System.out.println(StringUtils.isNotEmpty(s3)); // true
		
		System.out.println(StringUtils.isBlank(s1)); // true
		System.out.println(StringUtils.isBlank(s2)); // true
		System.out.println(StringUtils.isBlank(s3)); // true
		
		System.out.println(StringUtils.isNotBlank(s1)); // false
		System.out.println(StringUtils.isNotBlank(s2)); // false
		System.out.println(StringUtils.isNotBlank(s3)); // false
		
		String s4 = " \b\t\n "; // 下面2种trim 修饰后 返回都是 "", 如果是s1 第二个报错.
		System.out.println(StringUtils.trim(s2));
		System.out.println(s2.trim());
		
		System.out.println("你看那 漫山遍野, \r\n你还觉得孤单吗.");
		
		String s5 = "Hello";
		String s6 = "hello";
		System.out.println(StringUtils.equalsIgnoreCase(s5, s6)); //true
		System.out.println(s5.equalsIgnoreCase(s6)); // true
		
		System.out.println(StringUtils.equalsAny(s6, s1, s2, s3, s4, s5)); // false
		System.out.println(StringUtils.equalsAnyIgnoreCase(s6, s1, s2, s3, s4, s5)); // true
	}
         1.3 移除操作
	/**
	 *      *                 *
	 *     * *               * *
	 *    * * *             * * * 
	 *   * * * *  移除操作  * * * * 
	 *   
	 * 1. public static String removeStart(final String str, final String remove)
	 *    移除str中以remove开头的字符串
	 * 2. public static String removeStartIgnoreCase(final String str, final String remove)
	 *    同上,忽略大小写
     * 3. public static String removeEnd(final String str, final String remove)
     * 4. public static String removeEndIgnoreCase(final String str, final String remove) 这两个就不解释了
     * 5. public static String remove(final String str, final String remove)
     *    移除remove子串
     * 6. public static String removeIgnoreCase(String str, String remove)
     *    同上,忽略大小写
     * 7. public static String remove(final String str, final char remove)
     *    移除字符
     * 8. public static String removeAll(final String text, final String regex)
     *    移除所有满足正则表达式的子串, StingUtils.removeAll() 被 RegExUtils.removeAll() 替代了
     * 
     * 进入 原方法 可以看人家写的注解, 举了几个例子 很清晰.
     * 还有很多其他的像 indexOf、contains等等就不写了, 可以翻翻 原博主的链接, 瞄一眼 回忆一下.
	 */
	@Test
	public void 移除操作() {
		String s2 = RegExUtils.removeAll("A&lt;__&gt;\n&lt;__&gt;B", Pattern.compile("&lt;.*&gt;"));
		String s3 = RegExUtils.removeAll("A&lt;__&gt;\n&lt;__&gt;B", Pattern.compile("(?s)&lt;.*&gt;"));
		String s4 = RegExUtils.removeAll("A&lt;__&gt;\n&lt;__&gt;B", Pattern.compile("&lt;.*&gt;", Pattern.DOTALL));
		String s5 = RegExUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"));
		System.out.println("s2 = " + s2); // s2 = A\nB
		System.out.println("s3 = " + s3); // s3 = AB
		System.out.println("s4 = " + s4); // s4 = AB
		System.out.println("s5 = " + s5); // s5 = ABC123
	}
         1.4 替换操作
	/**
	 *      *                 *
	 *     * *               * *
	 *    * * *             * * * 
	 *   * * * *  替换操作  * * * * 
	 *   
	 * 1. public static String replaceOnce(final String text, final String searchString, final String replacement)
	 *    字符串替换,只替换第一个找到的。
	 * 2. public static String replaceOnceIgnoreCase(String text, String searchString, String replacement)
	 *    同上,忽略大小写
	 * 3. public static String replace(final String text, final String searchString, final String replacement)
	 *    替换所有
	 * 4. public static String replaceIgnoreCase(String text, String searchString, String replacement)
	 *    替换所有,忽略大小写
	 * 5. public static String replacePattern(final String source, final String regex, final String replacement)
	 *    满足正则表达式的字符串给替换掉
	 * 6. public static String replaceAll(final String text, final String regex, final String replacement)
	 *    同上,但是会全部替换掉
	 * 7. public static String replaceChars(final String str, final char searchChar, final char replaceChar)
	 *    替换所有字符, 只能单个字符
	 * 8. public static String replaceChars(final String str, final String searchChars, String replaceChars)
	 *    将searchChars在str中的所有字符替换掉, 可以是字符串
	 */
	@Test
	public void 替换操作() {
		StringUtils.replaceOnce("", "", "");
		StringUtils.replace("", "", "");
		RegExUtils.replacePattern("", "", "");
		RegExUtils.replaceAll("", "", "");
	}
         1.5 拼接操作
	/**
	 *      *                 *
	 *     * *               * *
	 *    * * *             * * * 
	 *   * * * *  拼接操作  * * * * 
	 *   
	 * 进入方法里 查看方法使用说明, 上面有很多例子, 下面随缘调几个例子 写
	 * 
	 * 1. public static <T> String join(final T... elements)
	 *    将参数进行拼接, 参数可以写 ①"我","想","你" ②数组 ③List ④Iterator
	 *    
	 * 2. public static String join(final Object[] array, final char separator)
	 *    将 数组里的元素拼接, 并且在每个元素后面再 拼接上 separator
	 *    
	 * 3. public static String repeat(final String str, final int repeat)
	 *    将str串重复repeat次连接起来,可以是负数。  可以认为 3的功能涵盖了 4
	 *    
     * 4. public static String repeat(final char ch, final int repeat)
     *    字符重复
	 */
	@Test
	public void 拼接操作() {
		String[] strings = "a.2.c.".split("\\.");
		System.out.println(Arrays.toString(strings)); // [a, 2, c]
		
		/* String类
		 * public static String join(CharSequence delimiter, CharSequence... elements)
		 * 将 elements 元素 用 delimiter 分割 拼接组成1个字符串
		 */
		String join = String.join("/", "a","b","c");
		System.out.println("join = " + join); // join = a/b/c
		join = String.join("a","b","c");
		System.out.println("join = " + join); // join = bac; 这么是乱序的呀
		join = String.join("", "a","b","c");
		System.out.println("join = " + join); // join = abc; 第一个参数写个空字符串好了.

		
		String str2 = StringUtils.join("a", "b", " ", "c");
		System.out.println("str2 = " + str2); // str2 = ab c
		String str3 = StringUtils.join("n", "", null, null, "b");
		System.out.println("str3 = " + str3); // str3 = nb
		
		/* 
		 * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c" 
		 * 官方给的描述 是不对啊.. 这个编译 就不通过了. 是版本问题?
		 */
		String[] strArr = {"a", "b", "c"};
		String str4 = StringUtils.join(strArr, ';');
		System.out.println("str4 = " + str4); // str4 = a;b;c
		
		// 第2个参数 不用写 也是一样的结果.
		Integer[] intArr = {1, 2, 3};
		String str5 = StringUtils.join(intArr, null);
		System.out.println("str5 = " + str5); // str5 = 123
		String str6 = StringUtils.join(intArr);
		System.out.println("str6 = " + str6); // str6 = 123
		
		List<Integer> list = Arrays.asList(intArr);
		String st7 = StringUtils.join(list, "-");
		System.out.println("st7 = " + st7); // st7 = 1-2-3
		Iterator<Integer> iterator = list.iterator();
		String str8 = StringUtils.join(iterator, "**");
		System.out.println("str8 = " + str8); // str8 = 1**2**3
		
		StringUtils.repeat('a', 1);
		StringUtils.repeat("", 1);
		StringUtils.repeat("", "", 1);
	}
         1.6 字符类型
	/**
	 * 1. public static boolean isAlpha(final CharSequence cs)
	 *    判断字符串是否由Unicode字母组成
     *
     * 2. public static boolean isAlphaSpace(final CharSequence cs)
     *    判断字符串是否由Unicode字母和空格组成
     * 
     * 3. public static boolean isAlphanumeric(final CharSequence cs)
     *    判断字符串是否由Unicode字母和数字组成
     * 
     * 4. public static boolean isAlphanumericSpace(final CharSequence cs)
     *    判断字符串是否由Unicode字母和数字以及空格组成
     *    
     * 5. public static boolean isNumeric(final CharSequence cs)
     *    判断字符串是否都由数字(Unicode digits)组成, "." 不算数字
     *
     * 6. public static boolean isNumericSpace(final CharSequence cs)
     *    判断字符串是否都由 数字和空格组成
     * 
     * 7. public static boolean isWhitespace(final CharSequence cs)
     *    判断字符串是否只由空格组成
     * 
     * 8. public static boolean isAllLowerCase(final CharSequence cs)
     *    判断字串串是否只包含小写字母
     * 
     * 9. public static boolean isAllUpperCase(final CharSequence cs)
     *    判断字串串是否只包含大写字母
	 */
	public void 字符类型() {
		StringUtils.isAlpha("");
		StringUtils.isAlphaSpace("");
		StringUtils.isAlphanumeric("");
		StringUtils.isAlphanumericSpace("");
		StringUtils.isNumeric("");
		StringUtils.isNumericSpace("");
		StringUtils.isWhitespace("");
		StringUtils.isAllLowerCase("");
		StringUtils.isAllUpperCase("");
	}
         1.7 前缀后缀
	/**
	 * 1. public static String getCommonPrefix(final String... strs)
	 *    获取字符串数组中相同的前缀
	 *    
	 * 2. public static boolean startsWith(final CharSequence str, final CharSequence prefix)
	 *    判断prefix是否为str的前缀
     * 
     * 3. public static boolean startsWithIgnoreCase(final CharSequence str, final CharSequence prefix)
     *    同上,忽略大小写
     * 
     * 4. public static boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings)
     *    是否其中一个字符串为str的前缀
     * 
     * 5. public static boolean endsWith(final CharSequence str, final CharSequence suffix)
     * 
     * 6. public static boolean endsWithIgnoreCase(final CharSequence str, final CharSequence suffix)
     * 
     * 7. public static boolean endsWithAny(final CharSequence sequence, final CharSequence... searchStrings)
     *    这三个就同startsWith
	 */
	public void 前缀后缀() {
		StringUtils.getCommonPrefix("", "");
		StringUtils.startsWith("", "");
		StringUtils.startsWithIgnoreCase("", "");
		StringUtils.startsWithAny("", "");
	}
         1.8 其他操作
	/**
	 * 1. public static String center(final String str, final int size)
	 *    获取新的字符串长度为size,让str位于中间,不足的补空格
	 *    如果 str 长度 > size, 则返回 str; 如果居中出现不对称, 以左为尊。
     * 
     * 2. public static String center(String str, final int size, final char padChar)
     *    同上,但是上面使用空格填充,这里使用padChar填充
     *    
     * 3. public static int countMatches(final CharSequence str, final CharSequence sub)
     *    字符串str中有多少个子串sub; 可以认为 此功能 涵盖了 4.
	 * 
     * 4. public static int countMatches(final CharSequence str, final char ch)
     *    字符串str中有多少个子字符ch
     *    
     * 5. public static String rotate(String str, int shift)
     *   将字符串往右旋转shift位
     *
     * 6. public static String reverse(final String str)
     *   翻转字符串 
	 */
	public void 其他操作() {
		
		StringUtils.center("", 1);
		StringUtils.center("", 1, 'a');
		StringUtils.center("", 1, "");
		
		StringUtils.countMatches("", 'a');
		StringUtils.countMatches("", "");
		
		StringUtils.rotate("", 1);
		StringUtils.reverse("");
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值