Checking for a not null, not blank String in Java

前端 未结 8 1632
猫巷女王i
猫巷女王i 2021-01-31 13:24

I am trying to check if a Java String is not null, not empty and not whitespace.

In my mind, this code should have been quite up for the job.

         


        
相关标签:
8条回答
  • 2021-01-31 14:06
    <% 
    System.out.println(request.getParameter("userName")+"*");
    
    if (request.getParameter("userName").trim().length() == 0 | request.getParameter("userName") == null) { %>
    <jsp:forward page="HandleIt.jsp" />
    <% }
     else { %>
    Hello ${param.userName}
    <%} %>
    
    0 讨论(0)
  • 2021-01-31 14:09

    I would choose isBlank() over isEmpty() because trim() creates a new String object that has to be garbage collected later. isBlank() on the other hand does not create any objects.

    0 讨论(0)
  • 2021-01-31 14:13

    Why can't you simply use a nested ternary operator to achieve this.Please look into the sample code public static void main(String[] args) { String s = null; String s1=""; String s2="hello"; System.out.println(" 1 "+check(s)); System.out.println(" 2 "+check(s1)); System.out.println(" 3 "+check(s2)); } public static boolean check(String data) { return (data==null?false:(data.isEmpty()?false:true)); }

    and the output is as follows

    1 false 2 false 3 true

    here the 1st 2 scenarios returns false (i.e null and empty)and the 3rd scenario returns true

    0 讨论(0)
  • 2021-01-31 14:15

    Is there a string that will make the isEmpty and isBlank behave differently in a test case?

    Note that Character.isWhitespace can recognize Unicode characters and return true for Unicode whitespace characters.

    Determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:

    • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').

    • [...]

    On the other hand, trim() method would trim all control characters whose code points are below U+0020 and the space character (U+0020).

    Therefore, the two methods would behave differently at presence of a Unicode whitespace character. For example: "\u2008". Or when the string contains control characters that are not consider whitespace by Character.isWhitespace method. For example: "\002".

    If you were to write a regular expression to do this (which is slower than doing a loop through the string and check):

    • isEmpty() would be equivalent to .matches("[\\x00-\\x20]*")
    • isBlank() would be equivalent to .matches("\\p{javaWhitespace}*")

    (The isEmpty() and isBlank() method both allow for null String reference, so it is not exactly equivalent to the regex solution, but putting that aside, it is equivalent).

    Note that \p{javaWhitespace}, as its name implied, is Java-specific syntax to access the character class defined by Character.isWhitespace method.

    Assuming there are none, is there any other consideration because of which I should choose isBlank and not use isEmpty?

    It depends. However, I think the explanation in the part above should be sufficient for you to decide. To sum up the difference:

    • isEmpty() will consider the string is empty if it contains only control characters1 below U+0020 and space character (U+0020)

    • isBlank will consider the string is empty if it contains only whitespace characters as defined by Character.isWhitespace method, which includes Unicode whitespace characters.

    1 There is also the control character at U+007F DELETE, which is not trimmed by trim() method.

    0 讨论(0)
  • 2021-01-31 14:16

    This simple code will do enough:

    public static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().equals("");
    }
    

    And the unit tests:

    @Test
    public void testIsNullOrEmpty() {
        assertEquals(true, AcdsUtils.isNullOrEmpty(""));
        assertEquals(true, AcdsUtils.isNullOrEmpty((String) null));
        assertEquals(false, AcdsUtils.isNullOrEmpty("lol    "));
        assertEquals(false, AcdsUtils.isNullOrEmpty("HallO"));
    }
    
    0 讨论(0)
  • 2021-01-31 14:22

    The purpose of the two standard methods is to distinguish between this two cases:

    org.apache.common.lang.StringUtils.isBlank(" ") (will return true).

    org.apache.common.lang.StringUtils.isEmpty(" ") (will return false).

    Your custom implementation of isEmpty() will return true.


    UPDATE:

    • org.apache.common.lang.StringUtils.isEmpty() is used to find if the String is length 0 or null.

    • org.apache.common.lang.StringUtils.isBlank() takes it a step forward. It not only checks if the String is length 0 or null, but also checks if it is only a whitespace string.

    In your case, you're trimming the String in your isEmpty method. The only difference that can occur now can't occur (the case you gives it " ") because you're trimming it (Removing the trailing whitespace - which is in this case is like removing all spaces).

    0 讨论(0)
提交回复
热议问题