Strings are the most used data type in Java. Almost every application in Java makes use of strings. That’s why strings are treated with special attention in Java. From time to time, new methods are added to String class so that working with strings becomes effortless and simple. join() in Java 8, chars() and codePoints() in Java 9, isBlank(), lines(), repeat(), strip(), stripLeading() and stripTrailing() in Java 11, indent(), transform(), describeConstable() and resolveConstantDesc() in Java 12, formatted(), stripIndent() and translateEscapes() in Java 15 are some new Java string methods.
In this post, we will see all the new methods added to String class from Java 8 to Java 17.
Java 8 New String Methods
Only one method is introduced to String class in Java 8 i.e. join().
join() :
This method is used to join the string elements separated by a delimiter. It has two versions. One which takes delimiter and an array of strings as arguments and another one which takes delimiter and an Iterable as arguments. Iterable may be the list of strings, set of strings etc…
import java.util.Arrays;
import java.util.List;
public class JavaNewStringMethods {
public static void main(String[] args) {
String languages = String.join("_", "Java", "HTML", "Python", "CSS", "PHP");
System.out.println(languages);
List<String> languageList = Arrays.asList("Java", "HTML", "Python", "CSS", "PHP");
languages = String.join(", ", languageList);
System.out.println(languages);
}
}
Output :
Java_HTML_Python_CSS_PHP
Java, HTML, Python, CSS, PHP
Java 9 New String Methods
chars() and codePoints() are the two new methods introduced in Java 9. Both return an IntStream.
chars() :
This method returns an IntStream of char values of the given string.
public class JavaNewStringMethods {
public static void main(String[] args) {
"ngeducate".chars().forEach(System.out::println);
}
}
Output :
110
103
101
100
117
99
97
116
101
codePoints() :
This method returns an IntStream of Unicode code points of the chars of the given string.
public class JavaNewStringMethods {
public static void main(String[] args) {
"ngeducate".codePoints().forEach(System.out::println);
}
}
Output :
110
103
101
100
117
99
97
116
101
Java 10 New String Methods
No new methods.
Java 11 New String Methods
There are 6 new string methods are introduced in Java 11. They are – isBlank(), lines(), repeat(), strip(), stripLeading() and stripTrailing().
isBlank() :
You can use this method to check whether given string is blank or not. A string is said to be blank if it is empty or contains only white spaces.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("".isBlank()); //Output : true
System.out.println(" ".isBlank()); //Output : true
System.out.println("\t \t".isBlank()); //Output : true
System.out.println("\n \n".isBlank()); //Output : true
System.out.println("STRING".isBlank()); //Output : false
System.out.println("String \t \n".isBlank()); //Output : false
}
}
lines() :
This method returns a stream of lines extracted from the given string. A line can be defined as a sequence of zero or more characters followed by a line terminator.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("\n\n".lines().count()); //Output : 2
System.out.println("abc \n xyz".lines().count()); //Output : 2
System.out.println("123 \n 456 \n".lines().count()); //Output : 2
System.out.println("abc \n 123 \n xyz".lines().count()); //Output : 3
}
}
repeat() :
This method returns the calling string repeated n times.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("1".repeat(5));
System.out.println("abc".repeat(3));
System.out.println("1a2b3c".repeat(2));
}
}
Output :
11111
abcabcabc
1a2b3c1a2b3c
strip() :
You can use this method to remove all leading and trailing white spaces of the given string.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println(" 1 ".strip());
System.out.println("\t A \t".strip());
System.out.println("\n A1 \n".strip());
System.out.println("1 A".strip());
}
}
Output :
1
A
A1
1 A
stripLeading() :
This method removes only leading white spaces of a string.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println(" 1".stripLeading());
System.out.println(" 11".stripLeading());
System.out.println(" 111".stripLeading());
System.out.println(" 1111".stripLeading());
}
}
Output :
1
11
111
1111
stripTrailing() :
This method removes only trailing white spaces of a string.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println(" 1 ".stripTrailing());
System.out.println(" 11 ".stripTrailing());
System.out.println(" 111 ".stripTrailing());
System.out.println("1111 ".stripTrailing());
}
}
Output :
1
11
111
1111
Java 12 New String Methods
Four more new methods are introduced in Java 12. They are – indent(), transform(), describeConstable() and resolveConstantDesc().
indent() :
This method applies indentation for each line of the given string according to supplied value.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("123\nabc\nABC".indent(4));
}
}
Output :
123
abc
ABC
transform() :
This method applies given Function to the string.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("string".transform(String::toUpperCase));
System.out.println("abc".transform(str -> str.concat("xyz"))
.transform(String::toUpperCase));
}
}
Output :
STRING
ABCXYZ
From Java 12, String class implements two more interfaces – Constable and ConstantDesc. From these two interfaces, String class inherits two more methods – describeConstable() from Constable and resolveConstantDesc() from ConstantDesc.
describeConstable() :
This method returns an Optional containing nominal descriptor for the given string, which is the string itself.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("123".describeConstable().get());
System.out.println("abc".describeConstable().get());
System.out.println("ABC".describeConstable().get());
}
}
Output :
123
abc
ABC
resolveConstantDesc() :
This method resolves the given string as ConstantDesc and returns the string itself.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("Java".resolveConstantDesc(MethodHandles.lookup()));
System.out.println("Python".resolveConstantDesc(MethodHandles.lookup()));
System.out.println("JavaScript".resolveConstantDesc(MethodHandles.lookup()));
}
}
Output :
Java
Python
JavaScript
Java 13 New String Methods
No new methods.
Java 14 New String Methods
No new methods.
Java 15 New String Methods
formatted() :
This method formats the given string with the supplied arguments. This method is similar to String.format(this, args).
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println("1) %s 2) %s 3) %s ".formatted("Java", "Python", "JavaScript"));
}
}
Output :
1) Java 2) Python 3) JavaScript
stripIndent() :
This method removes indentation of the given string at the beginning and at the end of every line.
public class JavaNewStringMethods {
public static void main(String[] args) {
System.out.println(" 123".stripIndent());
System.out.println("123 ".stripIndent());
System.out.println(" 123 ".stripIndent());
}
}
Output :
123
123
123
translateEscapes() :
This method returns a string with escape sequences translated as if in a string literal.
public class JavaNewStringMethods {
public static void main(String[] args) {
String str = "Tab \t Next Line \n Space \s Single Quote \' Double Quote \" ";
System.out.println(str.translateEscapes());
}
}
Output :
Tab Next Line
Space Single Quote ‘ Double Quote “
Java 16 New String Methods
No new methods.
Java 17 New String Methods
No new methods.
0 Comments