Signup/Sign In
LAST UPDATED: SEPTEMBER 20, 2021

New String Class Functions in Java 11

    Java 11 is the second long time support release after Java 8.

    We are all very familiar with Java 8 and its amazing features, but 4 years have passed since the release of Java 8 and Java has made a few major steps forward, in terms of syntax and features. And now, it's high time to do some catching up with the new features because we already have Java 9, upgraded by Java 10 and Java 11 now, which is an important release because just like Java 8, it's going to be a longer version of support.

    With Java 11, Oracle JDK will no longer be free. Java 10 was the last free Oracle JDK download.

    This version of Java has several new features including:

    • The Java file can be executed in a single command

    • Addition of new methods to be used in the String Class.

    • Local-variable syntax is provided with the Lambda parameters

    • Nested Applied Access Control

    • HTTP Client

    • Reading / Writing Strings to and from a file

    • Flight recordings

    New methods in the String Class

    Following are the new methods added in the String class in Java 11:

    1. isBlank()

    2. lines()

    3. strip(), stripLeading() and stripTrailing()

    4. repeat()

    Several new methods have been added to the String Class. Let's cover it in detail:

    1. isBlank() method:

    The isBlank() method returns true if the string is empty or contains only white spaces, otherwise false.

    Syntax:

    public boolean isBlank()

    Refer to the below program-1. Created input string with contents "WelcometoStudyTonight" and it is not blank. Hence, this method should return false.

    String input = "WelcometoStudyTonight";
    boolean isBlank = input.isBlank();
    System.out.println(isBlank);
    


    false

    Refer to the code below where we have created empty input string and then used the isBlank() which returns true.

    String input2 = "";
    isBlank = input2.isBlank();
    System.out.println(isBlank);


    true

    In the code example below, we have created a string variable with just some blank spaces, again the isBlank() method will return true.

    String input3 = "   ";
    isBlank = input3.isBlank();
    System.out.println(isBlank);


    true

    2. lines() method:

    This method breaks the given string into rows.

    Syntax:

    public Stream lines()

    Below example has a single line in string:

    String  value = "Welcome to StudyTonight";
    Stream stream = value.lines();
    Stream.forEach(line-> System.out.println(line));


    Welcome to StudyTonight

    Only one line is printed as expected because the input has no end of the line.

    In the code below, we have a string with multiple lines.

    String s1 = " This is \n an article on \n Java 11";
    Stream s2 =  s1.lines();
    s2.forEach(line -> System.out.println(line));


    This is
    an article on
    Java 11

    Replacing \n with \r will also provide the same output.

    String s1 = " This is \r an article on \r Java 11";
    Stream s2 =  s1.lines();
    s2.forEach(line -> System.out.println(line));


    This is
    an article on
    Java 11

    3. strip(), stripLeading() and stripTrailing() methods

    The strip() method removes white space from both ends of the given string.

    The strip() method is considered to appear in the "Unicode-aware" decoding

    Unicode was not generated at the time trim() was introduced. Now, the new strip() method removes all types of spaces. (Character.isWhiteSpace(c) is used to know whether Unicode is whitespace or not).

    public class StringStripping
    {
        public static void main(String[] args) 
        {
            String input = "   welcome to studytonight   ";
            System.out.println("Input string length : " + input.length());
            String strippedStr = input.strip();
            System.out.println("Stripped string length : " + strippedStr.length());
        }
    }


    Input string length: 29
    Stripped string length:23

    The stripLeading() method returns a new string with all the leading spaces removed from the given string. Leading white/blank spaces refers to the white spaces before the string.

    String input = "   Welcome   ";
    System.out.println("Input string length : " + input.length());
    String sStr = input.stripLeading();
    System.out.println("Stripped string length : " + sStr.length());
    System.out.println("Input String : "+input);
    System.out.println("Output String : "+sStr);


    Input string length: 13
    Stripped string length: 10
    Input String: Welcome
    Output String: Welcome

    The stripTrailing() method returns a new string with all the trailing blank spaces removed. The trailing white spaces means if there are white/blank spaces after the string characters end.

    String input = "   mohit   ";
    System.out.println("Input string length : " + input.length());
    String stripStr = input.stripTrailing();
    System.out.println("Stripped string length : " + stripStr.length());
    System.out.println("Input String :"+input);
    System.out.println("Output String :"+stripStr);


    Input string length: 11
    Stripped string length: 8
    Input String : mohit
    Output String: mohit

    4. repeat(int) method:

    It is used to simply multiply or repeat a string several times as specified by the int parameter.

    public class Main {
    public static void main(String[] args) throws Exception
    String s = "%".repeat(2);
    System.out.println(s); //prints %%
    
    


    %%

    Conclusion:

    With this, we have covered the new String functions introduced in Java 11. These functions comes in handy when you are working with strings.

    If we missed any function, please do share it with us in the comment section.

    You may also like:

    A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS