This tutorial shows an example of usage of String new methods in the java11 String class
- isBlank()
- lines()
- repeat()
- strip()
- stripLeading()
- stripTrailing()
These methods works on Instance of a java string
How to check empty String in java11 using the isBlank method
This example is another to check blank string check in java11
It introduced the isBlank() method
that checks empty strings or white spaces and returns true.
It returns false if the string contains a non-empty string.
Here is a java11 isBlank
example
public class Java11StringTest {
public static void main(String[] args) {
String str="";
System.out.println(str.isBlank()); // true
String str1=" ";
System.out.println(str1.isBlank()); // true
String str2=" abc ";
System.out.println(str2.isBlank()); // false
String str3=" \t ";
System.out.println(str3.isBlank()); // true
String str4=" \n ";
System.out.println(str4.isBlank()); // true
}
}
How to check the number of lines in java11 with the lines method
It has lines() method to check the number of lines of a given string.
Normally, a line is a group of characters or words separated by a line break.
lines()
method returns a stream of lines
public Stream<String> lines()
Here is an example to find the count of lines of a given string in java.
public class Java11StringTest {
public static void main(String[] args) {
String str="hello \n world";
System.out.println(str.lines().count()); // 2
String str1="hello \n world \n ";
System.out.println(str1.lines().count()); // 3
String str2=" hello world ";
System.out.println(str2.lines().count()); //1
String str3=" hello \n \t world";
System.out.println(str3.lines().count()); //2
String str4=" Hello \n world \n welcome ";
System.out.println(str4.lines().count()); //3
}
}
Here is another example to print the string lines
import java.util.stream.Stream;
public class Java11StringTest {
public static void main(String[] args) {
String str="hello \n world";
Stream<String> strs=str.lines();
strs.forEach(st->System.out.println(st));
}
}
Output:
hello
world
java repeat string multiple times using repeat method
java11 has a repeat() method that repeats the given string with argument times.
Syntax:
public String repeat(int count)
Here is an example
import java.util.stream.Stream;
public class Java11StringTest {
public static void main(String[] args) {
String str = "a";
System.out.println(str.repeat(3)); // aaa
String str1 = "Hello ";
System.out.println(str1.repeat(3)); //Hello Hello Hello
}
}
remove leading and trailing spaces using strip in java11
The strip
method removes leading and trailing spaces for a given string in java11.
Before java11, trim() method used to remove the spaces for a string.
strip
method works with Unicode characters, trim does not work with Unicode chars.
It does not remove the spaces in middle.
Here is an example of java11 strip method
import java.util.stream.Stream;
public class Java11StringTest {
public static void main(String[] args) {
String str = " Hello ";
System.out.println(str.strip());
String str1 = "Hello ";
System.out.println(str1.strip());
String str2 = " Hello";
System.out.println(str2.strip());
String str3 = " Hello world ";
System.out.println(str3.strip());
}
}
Output:
Hello
Hello
Hello
Hello world
How to remove leading spaces using stripLeading in java11
The stripLeading
method removes leading or beginning spaces for a given string in java11.
Here is an example for the java11 stripLeading
method
import java.util.stream.Stream;
public class Java11StringTest {
public static void main(String[] args) {
String str = " Hello ";
System.out.println(str.stripLeading());
String str1 = "Hello ";
System.out.println(str1.stripLeading());
String str2 = " Hello";
System.out.println(str2.stripLeading());
String str3 = " Hello world ";
System.out.println(str3.stripLeading());
}
}
Output:
Hello
Hello
Hello
Hello world
How to remove trailing spaces using stripTrailing in java11
The stripTrailing
method removes trailing or end spaces for a given string in java11.
Here is an example for the java11 stripTrailing
method
import java.util.stream.Stream;
public class Java11StringTest {
public static void main(String[] args) {
String str = " Hello ";
System.out.println(str.stripTrailing());
String str1 = "Hello ";
System.out.println(str1.stripTrailing());
String str2 = " Hello";
System.out.println(str2.stripTrailing());
String str3 = " Hello world ";
System.out.println(str3.stripTrailing());
}
}
Output:
Hello
Hello
Hello
Hello world