apache.commons 之 StringUtils 应用

apache.commons 之 StringUtils 应用

if (StringUtils.isNotEmpty(reminder)) {
    for (String str : reminder.split("#")) {
        int flag = context.indexOf(str);
        if (flag == -1) {
            mapReminder.put(str, false);
        } else {
            mapReminder.put(str, true);
        }
    }
}

package com.liwei.core.string;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;

/** * Created by Liwei on 2016/3/2. * * * 参考资料:StringUtils的isBlank与isEmply - 少爵 - 博客园 http://www.cnblogs.com/XiaoGer/archive/2011/11/06/2238227.html 最最关键的一点就是:如果是 “ ”,用 isBlank 返回的是 true,用 isEmply 返回的是 false; */
public class StringUtilsTest {



    /** * */
    @Test
    public void test001(){
        boolean result1 = StringUtils.isEmpty(" ");// false
        System.out.println(result1);

        boolean result2 = StringUtils.isBlank(" ");// true
        System.out.println(result2);
    }


    /** * */
    @Test
    public void test002(){
        boolean result1 = StringUtils.isEmpty("\t");// false
        System.out.println(result1);

        boolean result2 = StringUtils.isBlank("\t");// true
        System.out.println(result2);
    }


    /** * */
    @Test
    public void test003(){
        boolean result1 = StringUtils.isEmpty(" ");// false
        System.out.println(result1);

        boolean result2 = StringUtils.isBlank(" ");// true
        System.out.println(result2);
    }


    /** * 1、 * 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 * 只有这两条标准 */
    @Test
    public void testIsEmpty(){
        boolean result1 = StringUtils.isEmpty(null); // true
        System.out.println("StringUtils.isEmpty(null); => " + result1);

        boolean result2 = StringUtils.isEmpty(""); // true
        System.out.println("StringUtils.isEmpty(\"\"); => " + result2);

        boolean result3 = StringUtils.isEmpty(" "); //false //注意在 StringUtils 中空格作非空处理
        System.out.println("StringUtils.isEmpty(\" \"); => " + result3);

        boolean result4 = StringUtils.isEmpty(" "); //false
        System.out.println("StringUtils.isEmpty(\" \"); => " + result4);

        boolean result5 = StringUtils.isEmpty("bob"); //false
        System.out.println("StringUtils.isEmpty(\"bob\"); => " + result5);

        boolean result6 = StringUtils.isEmpty(" bob "); // false
        System.out.println("StringUtils.isEmpty(\" bob \"); => " + result6);
    }

    /** * 2、 * 判断某字符串是否非空,等于 !isEmpty(String str) * (不用过多理解,只要理解上面那条就可以了) */
    @Test
    public void testIsNotEmpty(){
        boolean result1 = StringUtils.isNotEmpty(null); //false
        boolean result2 = StringUtils.isNotEmpty(""); //false
        boolean result3 = StringUtils.isNotEmpty(" "); //true
        boolean result4 = StringUtils.isNotEmpty(" "); //true
        boolean result5 = StringUtils.isNotEmpty("bob"); //true
        boolean result6 = StringUtils.isNotEmpty(" bob "); //true

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
        System.out.println(result5);
        System.out.println(result6);
    }

    /** * 3、 * 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成 */
    @Test
    public void testIsBlank(){
        boolean result1 = StringUtils.isBlank(null); // true
        boolean result2 = StringUtils.isBlank(""); // true
        boolean result3 = StringUtils.isBlank(" "); // true
        boolean result4 = StringUtils.isBlank(" "); // true
        boolean result5 = StringUtils.isBlank("\t \n \f \r"); // true //对于制表符\t、换行符\n、换页符\f和回车符\r
        boolean result6 = StringUtils.isBlank("\n \n \n \n"); // 均识为空白符
        boolean result7 = StringUtils.isBlank("\b"); // false //"\b"为单词边界符
        boolean result8 = StringUtils.isBlank("bob"); // false
        boolean result9 = StringUtils.isBlank(" bob "); // false

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
        System.out.println(result5);
        System.out.println(result6);
        System.out.println(result7);
        System.out.println(result8);
        System.out.println(result9);
    }

    /** * 4、判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成,等于 !isBlank(String str) */
    @Test
    public void testIsNotBlank(){
        boolean result1 = StringUtils.isNotBlank(null); // false

        boolean result2 = StringUtils.isNotBlank(""); // false

        boolean result3 = StringUtils.isNotBlank(" "); // false

        boolean result4 = StringUtils.isNotBlank(" "); // false

        boolean result5 = StringUtils.isNotBlank("\t \n \f \r"); // false

        boolean result6 = StringUtils.isNotBlank("\b"); // true

        boolean result7 = StringUtils.isNotBlank("bob"); // true

        boolean result8 = StringUtils.isNotBlank(" bob "); // true

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
        System.out.println(result5);
        System.out.println(result6);
        System.out.println(result7);
        System.out.println(result8);
    }
}

你可能感兴趣的:(commons)