Say Goodbye to `StringUtil` by Optimizing Your Code with Java’s New String API
Each major update to the Java programming language has introduced many new features and improvements.
Some new methods have been introduced in the String to better meet the needs of development and improve programming efficiency.
Let’s look at some of the new methods and see how they can replace the role of the StringUtil.
As I tested myself, I think those methods are available on these Java versions below:
Java 11:
repeat(): return a new string, the string is formed by the original string repeated the specified number of times.isBlank(): check if the string is a sequence of blank characters, that is, the length of 0 or only contains space characters.lines(): return a stream of strings separated by lines.strip(): returns a new string formed by removing leading and trailing spaces from the original string.stripLeading(): return a new string, the string is the original string after removing the leading space formed.stripTrailing(): return a new string, the string is the original string after removing trailing spaces.
Java 15:
formatted(): format the string with the specified arguments and return the formatted string.translateEscapes(): converts Java escape sequences to their corresponding characters and returns the converted string.transform(): This method applies a function to a string and returns the result of the function.
Let’s also see some examples of how to use those new methods.
1. Repeat
String str = "malvin";
String repeatedStr = StringUtils.repeat(str, 3);
System.out.println(repeatedStr);
String repeatedStr2 = str.repeat(5);
System.out.println(repeatedStr2);
//result
//malvinmalvinmalvin
//malvinmalvinmalvinmalvinmalvin2. isBlank
String str1 = "";
String str2 = " ";
String str3 = " \t ";
System.out.println(StringUtils.isBlank(str1));
System.out.println(StringUtils.isBlank(str2));
System.out.println(StringUtils.isBlank(str3));
System.out.println(str1.isBlank());
System.out.println(str2.isBlank());
System.out.println(str3.isBlank());
//result
//true
//true
//true
//true
//true
//true3. lines
String str = "Hello\nWorld";
Arrays.stream(StringUtils.split(str, "\n")).forEach(System.out::println);
Stream<String> lines = str.lines();
lines.forEach(System.out::println);
//result
//Hello
//world
//Hello
//world4. strip
String str1 = " abc ";
String str2 = "\t def \n";
System.out.println(StringUtils.strip(str1));
System.out.println(StringUtils.strip(str2));
System.out.println(str1.strip());
System.out.println(str2.strip());
//result
//abc
//def
//abc
//def5. stripLeading/stripTrailing
String str1 = " abc ";
String str2 = " def ";
System.out.println(StringUtils.stripEnd(str1, null));
System.out.println(StringUtils.stripEnd(str2, null));
System.out.println(str1.stripTrailing());
System.out.println(str2.stripTrailing());
System.out.println(StringUtils.stripStart(str1, null));
System.out.println(StringUtils.stripStart(str2, null));
System.out.println(str1.stripLeading());
System.out.println(str2.stripLeading());
//result
// abc
// def
// abc
// def
//abc
//def
//abc
//def
6. formatted
String str = "My name is %s, I'm %d years old.";
String formattedStr = str.formatted( "Malvin", 30);
System.out.println(formattedStr);
//result
//My name is Malvin, I'm 30 years old.7. translateEscapes
String str = "Hello\\nWorld\\nJava";
String translatedStr = str.translateEscapes();
System.out.println(translatedStr);
//result
//Hello
//World
//Java8. transform
String str = "Hello World";
String result = str.transform(s -> s + " Java!");
System.out.println(result);
//result
//Hello World Java!That’s it. Thanks for watching. Feel free to comment if it helps you.






