zoukankan      html  css  js  c++  java
  • StringUtils常用方法介绍

    要使用StringUtils类,首先需要导入:import org.apache.commons.lang.StringUtils;这个包

    在maven项目中需要添加下面这个依赖:

        <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
            </dependency>

    它的常用方法有:

    StringUtils.isEmpty(str):

    判断字符串是否为"",null

    源码:

         * @param str  the String to check, may be null
         * @return <code>true</code> if the String is empty or null
         */
        public static boolean isEmpty(String str) {
            return str == null || str.length() == 0;
        }

    代码示例:

            String s1="";
            String s2="  ";
            String s3;
            String s4=null;
            String s5="曾阿牛";
            
            System.out.println(StringUtils.isEmpty(s1));//s1="";true
            System.out.println(StringUtils.isEmpty(s2));//s2="  ";false
            //System.out.println(StringUtils.isEmpty(s3));//s3;the local variable s3 may not have been initialized
            System.out.println(StringUtils.isEmpty(s4));//s4=null;true
            System.out.println(StringUtils.isEmpty(s5));//s5="曾阿牛";false

    StringUtils.isBlank(str):

    判断字符串是否为"","       ",null

    源码:

         * @param str  the String to check, may be null
         * @return <code>true</code> if the String is null, empty or whitespace
         * @since 2.0
         */
        public static boolean isBlank(String str) {
            int strLen;
            if (str == null || (strLen = str.length()) == 0) {
                return true;
            }
            for (int i = 0; i < strLen; i++) {
                if ((Character.isWhitespace(str.charAt(i)) == false)) {
                    return false;
                }
            }
            return true;
        }

    代码示例:

            System.err.println(StringUtils.isBlank(s1));//s1="";true
            System.err.println(StringUtils.isBlank(s2));//s2="  ";true
            System.err.println(StringUtils.isBlank(s4));//s4=null;true
            System.err.println(StringUtils.isBlank(s5));//s5="曾阿牛";false
  • 相关阅读:
    键盘弹出后上提view隐藏后下拉view还原并修改scroll过程中旋转屏幕到竖屏view显示错误
    iOS UI Element Usage
    ios notification
    retain和copy还有assign的区别
    UVA-11728 Alternate Task
    UVA-11490 Just Another Problem
    UVA-10127 Ones (数论)
    UVA-10710 Skyscraper Floors (找规律+幂取模)
    UVA-10539 Almost Prime Numbers
    UVA-10692 Huge Mods
  • 原文地址:https://www.cnblogs.com/luzhanshi/p/10695922.html
Copyright © 2011-2022 走看看