Java 11: New Methods in String Class

Filed Under: Java

Java 11 doesn’t have a lot of language specific features. So, it was surprising to see 6 new methods getting introduced in Java String Class.

Java 11 String Class New Methods

Let’s look at these new String class methods one by one.

  1. isBlank(): This method returns true if the string is empty or contains only white spaces code points.
  2. 
    String s = "abc";
    System.out.println(s.isBlank());
    s = "";
    System.out.println(s.isBlank());
    s = "\t \t";
    System.out.println(s.isBlank());
    

    Output:

    
    false
    true
    true
    

    Notice that “\t” is considered as a white space character codepoint in Unicode.

    Java String IsBlank()

    Java String IsBlank()

    I am using jShell to execute the code snippets without actually creating a java file.

  3. lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as \n, \r etc.
  4. 
    String s1 = "Hi\nHello\rWassup";
    System.out.println(s1);
    List lines = s1.lines().collect(Collectors.toList());
    System.out.println(lines); 
    

    Output:

    Java String lines() Function

    Java String lines() Function

    This method is useful to process multi-line strings with one line at a time.

  5. strip(), stripLeading(), stripTrailing(): These methods are used to strip whitespaces from the string. As the name suggests, strip() will remove leading and trailing whitespaces. However, stripLeading() will remove only leading whitespaces and stripTrailing() will remove only trailing whitespaces.
  6. 
    String s2 = "  Hello,  \tWorld\t ";
    System.out.println("#" + s2 + "#");
    System.out.println("#" + s2.strip() + "#");
    System.out.println("#" + s2.stripLeading() + "#");
    System.out.println("#" + s2.stripTrailing() + "#");
    

    Output:

    
    #  Hello,  	World	 #
    #Hello,  	World#
    #Hello,  	World	 #
    #  Hello,  	World#
    
    Java String strip(), stripLeading(), stripTrailing() functions

    Java String strip(), stripLeading(), stripTrailing()

  7. repeat(int n): This method returns a new string whose value is the concatenation of this string repeated ‘n’ times.
  8. 
    String s3 = "Hello\n";
    System.out.println(s3.repeat(3));
    s3 = "Do";
    System.out.println(s3.repeat(2));
    

    Output:

    Java String repeat()

    Java String repeat()

Conclusion

Java String class has a lot of utility methods. However, all these new utility methods will be very useful because we won’t have to worry about writing them ourselves and thinking about whether they cover all the rare scenarios related to different types of Unicode characters or not.

You can look at more String examples at our GitHub Repository.

Reference: Java 11 String Class API Doc

Comments

  1. Sai Krishna Sahu says:

    Pankaj, Can you create a new link on the side panel for Java 12 features. It is hard to find for a quick navigation.

  2. tiendm says:

    awesome! Thanks for introduction

  3. Bala says:

    Awesome, Keep good work!!!!!!!

  4. graju256 says:

    How do the Strip methods different from trim ones?

    1. Pankaj says:

      If you look at the trim() method documentation, it removes all leading and trailing spaces, where space is defined as any character whose codepoint is less than or equal to ‘U+0020’ (the space character).

      Whereas, strip() is using Character.isWhitespace() function to identify the white space and remove it.

      There might be a scenario where a whitespace character codepoint is greater than ‘U+0020’, then trim() will not remove it whereas strip() will remove it.

      I would suggest using strip() function rather than trim() to remove white spaces going forward.

  5. Janjo says:

    Hey Pankaj,

    Really appreciate what you do.

    This is the perfect place for learning about new features of latest Java release and much more.
    Kudos and Respect.

    1. Pankaj says:

      Thanks for the kind words, they push me to do even better in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages