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();
    }
}

猜你喜欢

转载自blog.csdn.net/tb9125256/article/details/81212946