org.apache.commons.lang3.StringUtils得使用

package com.zheting.my.utils;

import java.nio.charset.Charset;

import org.apache.commons.lang3.StringUtils;

public class MyStringUtils {

    /**
     * public static String abbreviate(String str, int maxWidth) 
     * 字符串 简写 缩写
     */
    public void test02() {
        System.out.println(StringUtils.abbreviate(null, 1)); // null
        System.out.println(StringUtils.abbreviate("", 4)); // "";
        System.out.println(StringUtils.abbreviate("abcdefg", 6)); // "abc...";
        System.out.println(StringUtils.abbreviate("abcdefg", 7)); // "abcdefg"
        System.out.println(StringUtils.abbreviate("abcdefg", 8)); // "abcdefg"
        System.out.println(StringUtils.abbreviate("abcdefg", 4)); // "a..."
        System.out.println(StringUtils.abbreviate("abcdefg", 3)); // IllegalArgumentException
    }

    /**
     * public static String abbreviate(String str, int offset, int maxWidth) 
     * 字符串 简写 缩写
     */
    public void test03() {
        System.out.println(StringUtils.abbreviate(null, 0, 4)); // null
        System.out.println(StringUtils.abbreviate("", 0, 4)); // ""
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", -1, 10));// "abcdefg..."
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 0, 10));// "abcdefg..."
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 1, 10));// "abcdefg..."
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 4, 10));// "abcdefg..."
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 5, 10));// "...fghi..."
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 6, 10));// "...ghij..."
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 8, 10));// "...ijklmno"
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 10, 10));// "...ijklmno"
        System.out.println(StringUtils.abbreviate("abcdefghijklmno", 12, 10));// "...ijklmno"
        System.out.println(StringUtils.abbreviate("abcdefghij", 0, 3));// IllegalArgumentException
        System.out.println(StringUtils.abbreviate("abcdefghij", 5, 6));// IllegalArgumentException

    }

    /**
     * public static String difference(String str1, String str2)
     * 比较两个字符串,返回字符串str2和str1不同的部分
     */
    public void test04() {
        System.out.println(StringUtils.difference(null, null));// null
        System.out.println(StringUtils.difference("", ""));// ""
        System.out.println(StringUtils.difference("", "abc"));// "abc"
        System.out.println(StringUtils.difference("abc", ""));// ""
        System.out.println(StringUtils.difference("abc", "abc"));// ""
        System.out.println(StringUtils.difference("abc", "ab"));// ""
        System.out.println(StringUtils.difference("ab", "abxyz"));// "xyz"
        System.out.println(StringUtils.difference("abcde", "abxyz"));// "xyz"
        System.out.println(StringUtils.difference("abcde", "xyz"));// "xyz"
    }

    /**
     * public static int indexOfDifference(CharSequence cs1, CharSequence cs2)
     * cs1和cs2比较,返回cs2与cs1开始不同的索引值
     */
    public void test05() {
        System.out.println(StringUtils.indexOfDifference(null, null));// -1
        System.out.println(StringUtils.indexOfDifference("", ""));// -1
        System.out.println(StringUtils.indexOfDifference("", "abc"));// 0
        System.out.println(StringUtils.indexOfDifference("abc", ""));// 0
        System.out.println(StringUtils.indexOfDifference("abc", "abc"));// -1
        System.out.println(StringUtils.indexOfDifference("ab", "abxyz"));// 2
        System.out.println(StringUtils.indexOfDifference("abcde", "abxyz"));// 2
        System.out.println(StringUtils.indexOfDifference("abcde", "xyz"));// 0
    }

    /**
     * public static boolean startsWith(CharSequence str, CharSequence prefix)
     * 判断str是否以prefix开头
     */
    public void test06() {
        System.out.println(StringUtils.startsWith(null, null));// true
        System.out.println(StringUtils.startsWith(null, "abc"));// false
        System.out.println(StringUtils.startsWith("abcdef", null));// false
        System.out.println(StringUtils.startsWith("abcdef", "abc"));// true
        System.out.println(StringUtils.startsWith("ABCDEF", "abc"));// false
    }

    /**
     * public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix) 
     * 判断str是否以prefix开头,但是忽略大小写
     */
    public void test07() {
        System.out.println(StringUtils.startsWithIgnoreCase(null, null));// true
        System.out.println(StringUtils.startsWithIgnoreCase(null, "abc"));// false
        System.out.println(StringUtils.startsWithIgnoreCase("abcdef", null));// false
        System.out.println(StringUtils.startsWithIgnoreCase("abcdef", "abc"));// true
        System.out.println(StringUtils.startsWithIgnoreCase("ABCDEF", "abc"));// true
    }

    /**
     * public static boolean startsWithAny(CharSequence string, CharSequence...searchStrings)
     * 第二个不定参数中的任何一个字符和第一个参数的开头相同,就返回true
     */
    public void test08() {
        System.out.println(StringUtils.startsWithAny(null, null));// false
        System.out.println(StringUtils.startsWithAny(null,new String[] { "abc" }));// false
        System.out.println(StringUtils.startsWithAny("abcxyz", null));// false
        System.out.println(StringUtils.startsWithAny("abcxyz",new String[] { "" }));// false
        System.out.println(StringUtils.startsWithAny("abcxyz",  new String[] { "abc" }));// true
        System.out.println(StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc" }));// true
    }

    /**
     * public static boolean endsWith(CharSequence str, CharSequence suffix) 
     * 判断str是否以suffix结尾
     */
    public void test09(){
        System.out.println(StringUtils.endsWith(null, null));// true
        System.out.println(StringUtils.endsWith(null, "def"));// false
        System.out.println( StringUtils.endsWith("abcdef", null));// false
        System.out.println(StringUtils.endsWith("abcdef", "def"));// true
        System.out.println( StringUtils.endsWith("ABCDEF", "def"));// false
        System.out.println( StringUtils.endsWith("ABCDEF", "cde"));// false
    }

    /**
     * public static boolean endsWithAny(CharSequence string,  CharSequence... searchStrings)
     * 第一个参数string以不定参数searchStrings中的任何一个结尾,返回true
     */
    public void test10(){
        System.out.println( StringUtils.endsWithAny(null, null));// false
        System.out.println( StringUtils.endsWithAny(null, new String[] {"abc"}));// false
        System.out.println( StringUtils.endsWithAny("abcxyz", null) );// false
        System.out.println( StringUtils.endsWithAny("abcxyz", new String[] {""}));// true
        System.out.println(StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}));// true
        System.out.println( StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}));// true
        System.out.println( StringUtils.endsWithAny("abcxyz", new String[] {null, "XYZ", "abc"}));// false
    }


    /**
     * public static String normalizeSpace(String str)
     * 去掉前后的空格,以及中间多余的空格,只保留一个空格
     */
    public void test11(){
        String str = "   aa     cc  bb dd            ee        "; //aa cc bb dd ee
        System.out.println(StringUtils.normalizeSpace(str));
    }

    /**
     * public static String appendIfMissing(String str, CharSequence suffix,  CharSequence... suffixes)
     * Appends the suffix to the end of the string if the string does not already end with any the suffixes.
     * 如果str不是以suffixes中的任何一个结尾,那么把suffix添加到str后面。区分大小写
     */
    public void test12(){
        //因为suffixes是不定参数,因此可以省略
        System.out.println(StringUtils.appendIfMissing(null, null));// null
        System.out.println(StringUtils.appendIfMissing("abc", null) );// "abc"
        System.out.println(StringUtils.appendIfMissing("", "xyz") );// "xyz"
        System.out.println(StringUtils.appendIfMissing("abc", "xyz"));// "abcxyz"
        System.out.println(StringUtils.appendIfMissing("abcxyz", "xyz"));// "abcxyz"
        System.out.println(StringUtils.appendIfMissing("abcXYZ", "xyz"));// "abcXYZxyz"
        System.out.println(StringUtils.appendIfMissing(null, null, null));// null
        System.out.println(StringUtils.appendIfMissing("abc", null, null));// "abc"
        System.out.println( StringUtils.appendIfMissing("", "xyz", null));// "xyz"
        System.out.println( StringUtils.appendIfMissing("abc", "xyz", new CharSequence[]{null}) );// "abcxyz"
        System.out.println(StringUtils.appendIfMissing("abc", "xyz", ""));// "abc"
        System.out.println( StringUtils.appendIfMissing("abc", "xyz", "mno"));// "abcxyz"
        System.out.println( StringUtils.appendIfMissing("abcxyz", "xyz", "mno"));// "abcxyz"  后缀本来就和添加的相同的,就不提添加
        System.out.println(StringUtils.appendIfMissing("abcmno", "xyz", "mno"));// "abcmno"
        System.out.println( StringUtils.appendIfMissing("abcXYZ", "xyz", "mno"));// "abcXYZxyz"
        System.out.println(StringUtils.appendIfMissing("abcMNO", "xyz", "mno") );// "abcMNOxyz"
    }

    /**
     * public static String appendIfMissingIgnoreCase(String str, CharSequence suffix, CharSequence... suffixes)
     * Appends the suffix to the end of the string if the string does not already end, case insensitive, with any of the suffixes.
     *  如果str不是以suffixes中的任何一个结尾,那么把suffix添加到str后面。不区分大小写
     */
    public void test13(){
        System.out.println(StringUtils.appendIfMissingIgnoreCase(null, null) );// null
        System.out.println( StringUtils.appendIfMissingIgnoreCase("abc", null) );// "abc"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("", "xyz"));// "xyz"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("abc", "xyz") );// "abcxyz"
        System.out.println(StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz"));// "abcxyz"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz"));// "abcXYZ"
        System.out.println(StringUtils.appendIfMissingIgnoreCase(null, null, null) );// null
        System.out.println(StringUtils.appendIfMissingIgnoreCase("abc", null, null));// "abc"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("", "xyz", null) );// "xyz"
        System.out.println(StringUtils.appendIfMissingIgnoreCase("abc", "xyz", new CharSequence[]{null}) );// "abcxyz"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("abc", "xyz", "") );// "abc"
        System.out.println(StringUtils.appendIfMissingIgnoreCase("abc", "xyz", "mno") );// "axyz"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz", "mno") );// "abcxyz"
        System.out.println( StringUtils.appendIfMissingIgnoreCase("abcmno", "xyz", "mno"));// "abcmno"
        System.out.println(StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz", "mno") );// "abcXYZ"
        System.out.println(StringUtils.appendIfMissingIgnoreCase("abcMNO", "xyz", "mno"));// "abcMNO"
    }

    /**
     * public static String prependIfMissing(String str,  CharSequence prefix, CharSequence... prefixes)
     * Prepends the prefix to the start of the string if the string does not already start with any of the prefixes.
     * 如果str以prefixs中的任何一个开头,那么把prefix添加到str的开头。区分大小写。注意prefix和str的开头相同,也不添加。
     */
    public void test14(){
         System.out.println(StringUtils.prependIfMissing(null, null));// null
         System.out.println(StringUtils.prependIfMissing("abc", null));// "abc"
         System.out.println(StringUtils.prependIfMissing("", "xyz"));// "xyz"
         System.out.println(StringUtils.prependIfMissing("abc", "xyz"));// "xyzabc"
         System.out.println(StringUtils.prependIfMissing("xyzabc", "xyz"));// "xyzabc"
         System.out.println(StringUtils.prependIfMissing("XYZabc", "xyz"));// "xyzXYZabc"

         System.out.println(StringUtils.prependIfMissing(null, null, null));// null
         System.out.println(StringUtils.prependIfMissing("abc", null, null));// "abc"
         System.out.println(StringUtils.prependIfMissing("", "xyz", null));// "xyz"
         System.out.println(StringUtils.prependIfMissing("abc", "xyz", new CharSequence[]{null}));// "xyzabc"
         System.out.println(StringUtils.prependIfMissing("abc", "xyz", "") );// "abc"
         System.out.println(StringUtils.prependIfMissing("abc", "xyz", "mno"));// "xyzabc"
         System.out.println(StringUtils.prependIfMissing("xyzabc", "xyz", "mno"));// "xyzabc"
         System.out.println(StringUtils.prependIfMissing("mnoabc", "xyz", "mno"));// "mnoabc"
         System.out.println(StringUtils.prependIfMissing("XYZabc", "xyz", "mno"));// "xyzXYZabc"
         System.out.println(StringUtils.prependIfMissing("MNOabc", "xyz", "mno"));// "xyzMNOabc"
    }

    /**
     * public static String prependIfMissingIgnoreCase(String str, CharSequence prefix,  CharSequence... prefixes)
     * Prepends the prefix to the start of the string if the string does not already start, case insensitive, with any of the prefixes.
     * 如果str以prefixs中的任何一个开头,那么把prefix添加到str的开头。不区分大小写。注意prefix和str的开头相同,也不添加。
     */
    public void test15(){
        System.out.println(StringUtils.prependIfMissingIgnoreCase(null, null));// null
        System.out.println(StringUtils.prependIfMissingIgnoreCase("abc", null));// "abc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("", "xyz"));// "xyz"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("abc", "xyz"));// "xyzabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("xyzabc", "xyz"));// "xyzabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("XYZabc", "xyz"));// "XYZabc"


        System.out.println(StringUtils.prependIfMissingIgnoreCase(null, null, null));// null
        System.out.println(StringUtils.prependIfMissingIgnoreCase("abc", null, null));// "abc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("", "xyz", null));// "xyz"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("abc", "xyz", new CharSequence[]{null}));// "xyzabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("abc", "xyz", ""));// "abc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("abc", "xyz", "mno"));// "xyzabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("xyzabc", "xyz", "mno"));// "xyzabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("mnoabc", "xyz", "mno"));// "mnoabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("XYZabc", "xyz", "mno"));// "XYZabc"
        System.out.println(StringUtils.prependIfMissingIgnoreCase("MNOabc", "xyz", "mno"));// "MNOabc"
    }

    /**
     * public static String toEncodedString(byte[] bytes,   Charset charset)
     * 把字节数组bytes按照指定的字符集编码,返回新的字符串。
     */
    public void test16(){
        System.out.println(StringUtils.toEncodedString("abc".getBytes(), Charset.forName("gbk")));
    }

    public static void main(String[] args) {
        new MyStringUtils().test16();
    }
}
### 回答1: org.apache.commons.lang3.stringutils.jar 是一个用于处理字符串的 Java 库。它属于 Apache Commons Lang 项目的一部分。 这个库提供了许多方便且常用的字符串处理方法。比如,它包含了处理空字符串的方法,如判断字符串是否为空或只包含空格、移除字符串中的空格等。它还提供了一些字符串操作的方法,如字符串的截取、连接、反转等。此外,它还提供了一些处理字符的方法,如判断字符串是否为数字、字母或空白字符、转换字符串大小写等。 org.apache.commons.lang3.stringutils.jar 还包含了一些处理字符串数组的方法。比如,它提供了根据某个字符或字符串将字符串数组连接成一个字符串的方法,也可以将字符串数组按照指定规则进行分割。 此外,这个库还提供了一些处理字符串的其他实用方法,如字符串的填充、重复、截断等。它还提供了一些转换字符串格式的方法,如将字符串转换为 camelCase、snake_case 或 kebab-case 等。 总结来说,org.apache.commons.lang3.stringutils.jar 是一个功能强大且实用的字符串处理库,它提供了丰富的方法来方便开发者对字符串进行各种操作和处理。使用这个库可以有效地简化开发过程,并提高代码的可读性和可维护性。同时,Apache Commons Lang 项目作为一个广泛使用的开源项目,拥有活跃的社区和可靠的技术支持,能够提供稳定可靠的使用体验。 ### 回答2: org.apache.commons.lang3.stringutils.jar 是一个用于字符串操作的Java库,它是Apache Commons项目的一部分。在这个库中,org.apache.commons.lang3.stringutils.jar 提供了许多实用的字符串处理方法和工具类。 首先,该库提供了一系列静态方法,可以对字符串进行常见的操作,例如判断字符串是否为空、去除字符串中的空格、判断字符串是否包含特定字符等。这些方法有助于简化开发人员对字符串的处理和验证。 其次,lib库还提供了各种字符串操作的工具类。例如,StringUtils类提供了一系列高效的字符串操作方法,如截取字符串、替换字符串、大小写转换等。此外,StringEscapeUtils类提供了用于转义HTML、XML和Java字符串的方法,有助于避免特殊字符引起的问题。 此外,该库还包含了一些用于字符串比较和匹配的工具类。例如,StringUtils类提供了进行字符串比较的方法,可以根据不同的比较规则进行字符串的比较。此外,RegExUtils类提供了一些用于正则表达式匹配的方法,可以方便地根据正则表达式进行模式匹配和替换。 总之,org.apache.commons.lang3.stringutils.jar 提供了丰富的字符串处理方法和工具类,可以简化开发人员对字符串的操作,并且提供了高效、易用的字符串处理解决方案。无论是开发web应用、桌面应用还是其他类型的应用,该库都是一个非常有价值的工具。 ### 回答3org.apache.commons.lang3.stringutils.jar 是一个 Java 库,它是 Apache Commons Lang 的一部分。这个库提供了一系列用于字符串处理的实用方法和工具类。 在 org.apache.commons.lang3.stringutils.jar 中,有很多常用的字符串处理方法,例如: - isBlank() 方法用于判断字符串是否为空白(即全部由空格、制表符、换行符等组成)。 - isEmpty() 方法用于判断字符串是否为空(即长度为0)。 - capitalize() 方法用于将字符串的首字母大写,并将其他字符保持不变。 - uncapitalize() 方法用于将字符串的首字母小写,并将其他字符保持不变。 - join() 方法用于将一个数组或集合中的元素以指定的分隔符连接起来,形成一个新的字符串。 - contains() 方法用于判断字符串是否包含指定的字符序列。 - substring() 方法用于获取字符串的子串。 - swapCase() 方法用于将字符串中的大小写字母进行转换,大写字母转换为小写字母,小写字母转换为大写字母。 除了上述方法外,org.apache.commons.lang3.stringutils.jar 还提供了很多其他实用的字符串处理方法,用于处理字符串的拆分、替换、删除、填充等操作。这些方法都被封装在 StringUtils 类中,可以直接调用使用。 在开发过程中,使用 org.apache.commons.lang3.stringutils.jar 可以方便地处理字符串,提高开发效率和代码质量。同时,由于是开源库,还可以根据需要进行修改和定制,使其更符合实际需求。 总之,org.apache.commons.lang3.stringutils.jar 是一个值得使用的字符串处理库,它提供了丰富的方法和工具类,可以简化字符串的处理过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值