String functions in Java 11

Reading Time: 3 minutes

In this blog, I will be talking about the new String functions introduced as part of  Java11. Java8 and Java 9 have amazed us already and now it’s time for Java11 to make some action.

Let’s first look at the string functions and then we will talk about them in detail.

1. isBlank() 
2. lines()
3. strip(), stripLeading(), stripTrailing()

4. repeat(int value)

Let’s talk about these methods in detail one by one.

  1. isBlank() – isBlank() is a method that would check if a String is blank or not. Unlike empty() which is already there, it would check for spaces in an empty String. So, if you have String which is empty but having spaces within it empty() would still return true. Although you can use Apache String library for this functionality but this can be achieved if you have started using Java11 already, this is one of the String methods introduced as part of Java11.
    Example –
    2. lines() – This method would return a stream of strings if you have a string with multiple lines or with \n for that matter, what this method would do is, it will convert the string into a stream which can further be converted to a List. This is a very handy method when you have a very large string with \n or newlines.Example –

    3. strip(), stripLeading(), stripTrailing() – strip() method would remove whitespaces from beginning and end of the string but wait a minute, we already have a trim() method present for that, then why do we need this strip(), strip() is Unicode aware evolution of trim(), i.e this method internally uses Character.isWhitespace() for whitespaces otherwise character with code point more than U+0020 won’t be considered for removing whitespaces. Similarly, we have stripLeading() and stripTrailing() for removing leading whitespaces and trailing whitespaces respectively.

    Example –

    4. repeat(int) – The repeat() method is a method which will repeat the String as many times as it is mentioned in the method argument. This method would be handy when you want to repeat the string as many times as you want but you don’t want to make it obvious or you don’t want to make it verbose, you would simply call the repeat method and achieve the results.

    Example –

    Pre-requisite –
    To practice the method above, you need to have Java11 or higher installed on your system. Please check your version before.

    Download Java 11 –

    What you learned?
    String functions which were released as part of Java11 and how important they could be to solve various problems.

    Note- Java11 onwards, they have two different JDK builds:

    Oracle’s JDK (commercial) – You can use this in development and testing for free, but if you use it in production you have to pay for it(purchase license from Oracle).
    Oracle’s OpenJDK (open source) – You can use this for free in any environment, like any open-source library.

    You can still download the Commerical Oracle JDK for free and use it for free in development/test environments.

    That’s pretty much it from the article, I have a GitHub repository for all the examples related to this article, feel free to fork it and start practicing the examples. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding 🙂


Written by 

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.