Regular expression for not empty

后端 未结 7 1258
执笔经年
执笔经年 2020-12-28 18:31

I need a Java regular expression, which checks that the given String is not Empty. However the expression should ingnore if the user has accidentally given whitespace in the

7条回答
  •  灰色年华
    2020-12-28 18:54

    It's faster to create a method for this rather than using regular expression

    /**
     * This method takes String as parameter
     * and checks if it is null or empty.
     * 
     * @param value - The value that will get checked. 
     * Returns the value of "".equals(value). 
     * This is also trimmed, so that "     " returns true
     * @return - true if object is null or empty
     */
    public static boolean empty(String value) {
        if(value == null)
            return true;
    
        return "".equals(value.trim());
    }
    

提交回复
热议问题