Testing.

Friday, 22 February 2019

Java update: New methods for String class in java latest version (java jdk-11)

According to Oracle's Java Document there are six new methods are introduced in String Class.

  1. isBlank()
  2. strip()
  3. stripTrailing()
  4. stripLeading()
  5. lines()
  6. repeat(int count)

1. isBlank()

This method checks blank Strings or white spaces Strings.

It returns true if the String is empty or only white spaces codepoints, otherwise returns false.

Example:
   public class IsBlankExample
   {
       public static void main(String args[])
       {
        String s = "";
        System.out.println(s.isBlank());
        s = "I am not an Empty String";
        System.out.println(s.isBlank());
        s = "\t\t";
        System.out.println(s.isBlank());
        s = "\n";
        System.out.println(s.isBlank());
       }
   }

Output:
true
false
true
true


Note:
According to the java if the specified character (unicode code point) is white space if and only if it satisfies one of the following criteria:

  • It is Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but it is not also a non- breaking space ('\u00A0', '\u2007', '\u202F')
  • It is '\t' , U+0009 HORIZONTAL TABULATION 
  • It is '\n\, U+000A LINE FEED
  • It is \u000B' , U+000B VERTICAL TABULATION
  • It is '\f', U+000C FORM FEED
  • It is '\r', U+000D CARRIAGE RETURN
  • It is '\u001C', U+001C FILE SEPARATOR
  • It is '\u001D', U+001D GROUP SEPARATOR
  • It is '\001E', U+001E RECORD SEPARATOR
  • It is '\u001F', U+001F UNIT SEPARATOR  

2. strip():


This method strips (trims) the white spaces from the beginning and the end of the String.

Note:
If String object is empty String or if all code points are white spaces then an empty String will be returned.

Example:

public class stripExample
   {
       public static void main(String args[])
       {
        String s = "    Hello,   \tBye\t   ";
        System.out.println("#",+s);
        System.out.println("#"+s.strip()+"#");
        s = "\t   \t     \n";
        System.out.println(s.isBlank());
       }
   }

Output:
#    Hello,   Bye
#Hello,   Bye#
true


3. stripTrailing():


This method strips (trims) only ending white spaces of the String.

Example:
public class stripTrailingExample
   {
       public static void main(String args[])
       {
        String s = "    Hello,   \tBye\t   ";
        System.out.println("#",+s);
        System.out.println("#"+s.stripTrailing()+"#");
        s = "\t   \t     \n";
        System.out.println(s.isBlank());
       }
   }


Output:
#    Hello,   Bye
#    Hello,   Bye#
true


4. stripLeading():


This method strips (trims) only beginning white spaces of the String.

Example:
public class stripLeadingExample
   {
       public static void main(String args[])
       {
        String s = "    Hello,   \tBye\t   ";
        System.out.println("#",+s);
        System.out.println("#"+s.stripLeading()+"#");
        s = "\t   \t     \n";
        System.out.println(s.isBlank());
       }
   }


Output:
#    Hello,   Bye
#Hello,   Bye   #
true


5. lines():


This method returns a stream of lines extracted from the Strings, separated by line terminators such as '\t', '\n' etc.

A line is either a sequence of zero or more characters followed by a line terminator, or it a sequence of followed by end of the String. A line does not includes the line terminator.

The stream returned by this method contains the lines from this string in the order in which the occur.

Example:
      import java.util.List;
      import java.util.stream.Collectores;
      public class linesExample
      {
          public static void main (String args[])
          {
           String s = "Hi\nHello\rBye"
           System.out.println(s);
           List lines = s.lines().collect(Collectors.toList());
           System.out.println(lines);
          }
      }

Output:
Hi
Hello
Bye
[Hi, Hello, Bye]


6. repeat(int count):


This method repeats the String as many times we passed to the int count value. Here count represents number of times Strings to be repeat.

It Returns an empty String if String is empty or count is zero.

If count is negative integer then it will throw IllegalArgumentException.

Example:
   public class repeatExample
   {
      public static void main(String args[])
      {
        String s = "Hello";
        System.out.println(s.repeat(3));
        s = "Bye";
        System.out.println(s.repeat(2));
      }
   }
Output:
Hello
Hello
Hello
Bye
Bye

No comments:

Post a Comment