Tuesday, April 23, 2019

JAVA 11 – New String methods


JAVA 11 – New String methods

In this blog, we will discuss about all six new methods which got introduced with JDK 11.

  • isBlank(): returns true if string is empty otherwise false. White spaces code will also be considered as empty string.

// Java 8 Predefine Functional Interface
             // The best way to learn JAVA 8 is to implement it wherever you can...
             BiConsumer<String, String> display = (msg1, msg2) -> System.out.println(msg1 + msg2);

             // local variable type inference
             var msg = "Welcome to waheedtechblog page";
             // String msg = "Welcome to waheedtechblog page"; both are same

             // Will return true if string is empty or any white spaces
             if (msg.isBlank()) {
                    display.accept("msg attribute is emty", msg);
             } else {
                    display.accept("1. Hello, ", msg);
             }

String emptyMsg = "";
display.accept("2. Is msg Empty: ", String.valueOf(emptyMsg.isBlank()));

             String whitespacesMsg = "\n\t";
display.accept("2. Is msg empty: ", String.valueOf(whitespacesMsg.isBlank()));
Output:
            1. Hello, Welcome to waheedtechblog page
2. Is msg Empty: true
3. Is msg empty: true

  • lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as \n, \r etc.

            String lineMsg = "Welcome\nto\nwaheedtechblog\npage";
       display.accept("3. Line Msg: \n", lineMsg);
       // line returns streams of line extracted from the String
       List<String> lines = lineMsg.lines().collect(Collectors.toList());
       System.out.println("4. line as list: " + lines);
Output:
       3. Line Msg:
Welcome
to
waheedtechblog
page
4. line as list: [Welcome, to, waheedtechblog, page]

  • repeat(n): This method returns the concatenated string of original string repeated the number of times in the argument.

var repeatMessage = "Welcome";
// returns a new string whose value is the concatenation of this string repeated
             // ‘n’ times.
             display.accept("9. Repeat msg: ", repeatMessage.repeat(5));
Output:
9. Repeat msg: WelcomeWelcomeWelcomeWelcomeWelcome

  • Strip(), StripLeading(),StripTrailing(): These method are used to strip white spaces from the String

                   var stripMsg = "    Welcome to   \t Waheedtechblog page     ";
             display.accept("5. Normal Print: ", "$" + stripMsg + " $");
             // As the name suggests, Strip() methods are used to strip whitespaces from the
             // string.
             display.accept("6. Using Strip(): ", "$" + stripMsg.strip() + "$");
             display.accept("7. Using StripLeading(): ", "$" + stripMsg.stripLeading() + "$");
display.accept("8. Using StripTrailing: ", "$" + stripMsg.stripTrailing() + "$");

Output:
5. Normal Print: $    Welcome to        Waheedtechblog page      $
6. Using Strip(): $Welcome to    Waheedtechblog page$
7. Using StripLeading(): $Welcome to    Waheedtechblog page     $
8. Using StripTrailing: $    Welcome to        Waheedtechblog page$

You can download the source code from my GitHub repository.

Happy Coding…!!!

No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...