Java 11 String New Methods
Java 11 added new methods to the String
class for better string handling. These methods are:
-
isBlank()
-
lines()
-
strip()
-
stripLeading()
-
stripTrailing()
-
repeat()
We will discuss each method with the help of examples.
Java String isBlank()
Method
This method is used to check whether the string is blank or not. It returns true if the string is empty or contains only white space codepoints, otherwise false. The syntax of the method is given below:
public boolean isBlank()
Basic Example:
In this example, we have two string values, one is non-blank and the other one is blank to check using the isBlank() method.
public class Main {
public static void main(String[] args){
String str = "Studytonight";
boolean r = str.isBlank();
System.out.println(r);
str = "";
r = str.isBlank();
System.out.println(r);
}
}
false
true
Java String lines()
Method
This method is used to get a stream of lines extracted from the string, separated by line terminators. The syntax of the method is given below:
public Stream<String> lines()
Basic Example:
In this example, we are getting a stream of the string by using lines()
method. The string is broken at termination points like "\n", "\r".
import java.util.stream.Stream;
public class Main {
public static void main(String[] args){
String str = "Studytonight \n is a \r technical \n portal";
Stream<String> lines = str.lines();
lines.forEach(System.out::println);
}
}
Studytonight
is a
technical
portal
Java String strip()
Method
This method is used to remove all the leading and trailing spaces from a string. If you want to remove only leading spaces then use the stripLeading()
method and for trailing, spaces use the stripTrailing()
method. The syntax of these methods are given below.
public String strip()
public String stripLeading()
public String stripTrailing()
Basic Example:
In this example, we are using strip()
method to remove all leading and trailing methods of a string.
public class Main {
public static void main(String[] args){
String str = " Studytonight portal ";
System.out.println(str);
String str2 = str.strip();
System.out.println(str2);
}
}
Studytonight portal
Studytonight portal
Another Example for strip()
:
In this example, we are using the stripLeading()
method to remove all the leading spaces from a string.
public class Main {
public static void main(String[] args){
String str = " Studytonight portal";
System.out.println(str);
String str2 = str.stripLeading();
System.out.println(str2);
}
}
Studytonight portal
Studytonight portal
Example 3:
In this example, we are using the stripTrailing()
method to remove all the trailing spaces from a string.
public class Main {
public static void main(String[] args){
String str = " Studytonight portal ";
System.out.println(str);
System.out.println(str.length());
String str2 = str.stripTrailing();
System.out.println(str2);
System.out.println(str2.length());
}
}
Studytonight portal
25
Studytonight portal
22
Java String repeat()
Method
This method is used to repeat a string at the specified time. It returns a string whose value is the concatenation of this string repeated specified times.
public String repeat(int count)
Basic Example:
In this example, we are using the repeat()
method to repeat the string multiple times:
public class Main {
public static void main(String[] args){
String str = "@";
System.out.println(str);
String str2 = str.repeat(3);
System.out.println(str2);
str = "mac";
System.out.println(str);
str2 = str.repeat(5);
System.out.println(str2);
}
}
@
@@@
mac
macmacmacmacmac