Java 11: Introduction to new String functions

Reading Time: 3 minutes

It’s been a while since the release of Java 11 and with that, some new functions have been introduced on String. These functions provide additional functionalities and computations to be done on String then the existing one. Through the course of this blog, we will explore those functions one by one along with some examples. 

These String functions have been added as a part of JDK 11:

  • String.repeat(int)
  • String.lines()
  • String.strip()
  • String.stripLeading()
  • String.stripTrailing()
  • String.isBlank()

Let us explore them :

1. String.repeat(int count)

This function returns a string whose value is the concatenation of this string repeated count times.

for example,

String repeatStr = "Hello";
System.out.println(repeatStr.repeat(3));
repeatStr = "World";
System.out.println(repeatStr.repeat(2));

output:

HelloHelloHello
WorldWorld

We can notice, Hello is printed 3 times as the count value is 3 and World is printed 2 times as the count value for it is 2.

2. String.lines()

It returns a stream of lines extracted from this string, separated by line terminators.

A line terminator is one of the following: a line feed character “\n” (U+000A), a carriage return character “\r” (U+000D), or a carriage return followed immediately by a line feed “\r\n” (U+000D U+000A).

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

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

for example,

String lineStr = "This is line 1 \nThis is line 2 \nThis is line 3";
System.out.println(lineStr);
//get list of lines from the above String
List<String> lines = lineStr.lines().collect(Collectors.toList());
System.out.println(lines);

output:

This is line 1 
This is line 2 
This is line 3
[This is line 1 , This is line 2 , This is line 3]

In the above example, we can see that the entire String has been divided into a list using the lines() function.

3. String.strip()

It returns a string whose value is this string, with all leading and trailing white space removed. If this String object represents an empty string, or if all code points in this string are white space, then an empty string is returned. Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to and including the last code point that is not a white space.

for example,

String stripStr = "  Strip demo  ";
System.out.println("--" + stripStr + "--");
System.out.println("--" + stripStr.strip() + "--");

output:

--  Strip demo  --
--Strip demo---

As seen in the second line of the output, we have leading and trailing white spaces removed.

4. String.stripLeading()

Returns a string whose value is this string, with all leading white space removed.
If this String object represents an empty string, or if all code points in this string are white space, then an empty string is returned.

Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to to and including the last code point of this string.

for example, 

String stripStr = "  Strip demo  ";
System.out.println("--" + stripStr + "--");
System.out.println("--" + stripStr.stripLeading() + "--");

output:

-- Strip demo --
--Strip demo --

As seen, all leading white spaces have been removed.

5. String.stripTrailing()

Returns a string whose value is this string, with all trailing white space removed.
If this String object represents an empty string, or if all characters in this string are white space, then an empty string is returned.

Otherwise, returns a substring of this string beginning with the first code point of this string up to and including the last code point that is not a white space.

for example,

String stripStr = "  Strip demo  ";
System.out.println("--" + stripStr + "--");
System.out.println("--" + stripStr.stripTrailing() + "--");

output:

-- Strip demo --
-- Strip demo--

In the above output, all trailing white spaces have been removed.

6. String.isBlank()

It returns true if the string is empty or contains only white space codepoints, otherwise false.

for example,

String str = "knoldus";
System.out.println(str.isBlank());
str = "";
System.out.println(str.isBlank());
str = "\n \t";
System.out.println(str.isBlank());

output:

false
true
true

Notice that “\t” and “\n” are considered as a white space character codepoint in Unicode.
If we give assign a String null value, then isBlank() on that will give us a NullPointerException.

These new String methods in JDK 11 provide a more consistent approach to handling white spaces. 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.

I hope, you have liked my blog. If you have any doubts or suggestions to make please drop a comment. Thanks!


References:
Oracle Official Doc


Knoldus-Scala-Spark-Services

+ posts

Vinisha Sharma is a software consultant having more than 6 months of experience. She thrives in a fast pace environment and loves exploring new technologies. She has experience with the languages such as C, C++, Java, Scala and is currently working on Java 8. Her hobbies include sketching and dancing. She believes Optimism and a learning attitude is the key to achieve success in your life

Written by 

Vinisha Sharma is a software consultant having more than 6 months of experience. She thrives in a fast pace environment and loves exploring new technologies. She has experience with the languages such as C, C++, Java, Scala and is currently working on Java 8. Her hobbies include sketching and dancing. She believes Optimism and a learning attitude is the key to achieve success in your life