avatarMalvin Lok

Summary

The website content discusses the introduction of new string manipulation methods in Java 11 and Java 15, which enhance programming efficiency and can replace the StringUtil utility.

Abstract

The article highlights the advancements in Java's string handling capabilities with the introduction of several new methods in Java 11 and Java 15. These methods, such as repeat(), isBlank(), lines(), strip(), stripLeading(), stripTrailing(), formatted(), translateEscapes(), and transform(), are designed to streamline string operations and improve code readability. The author provides examples to demonstrate how these new methods can be used in place of the StringUtil class, suggesting that developers can now write more concise and efficient code. The article encourages developers to adopt these new features to enhance their programming practices.

Opinions

  • The author believes that the new string methods in Java 11 and Java 15 can significantly improve development efficiency.
  • The examples provided suggest that the author sees the new methods as more intuitive and easier to use compared to the StringUtil class.
  • The author's use of before-and-after code snippets implies a preference for the new string methods for their ability to reduce the amount of code needed for common string operations.
  • By encouraging readers to comment on the article, the author appears to value community feedback and the sharing of experiences with the new string API.

Say Goodbye to `StringUtil` by Optimizing Your Code with Java’s New String API

Photo by Caspar Camille Rubin on Unsplash

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:

  1. repeat(): return a new string, the string is formed by the original string repeated the specified number of times.
  2. isBlank(): check if the string is a sequence of blank characters, that is, the length of 0 or only contains space characters.
  3. lines(): return a stream of strings separated by lines.
  4. strip(): returns a new string formed by removing leading and trailing spaces from the original string.
  5. stripLeading(): return a new string, the string is the original string after removing the leading space formed.
  6. stripTrailing(): return a new string, the string is the original string after removing trailing spaces.

Java 15:

  1. formatted(): format the string with the specified arguments and return the formatted string.
  2. translateEscapes(): converts Java escape sequences to their corresponding characters and returns the converted string.
  3. 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
//malvinmalvinmalvinmalvinmalvin

2. 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
//true

3. 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
//world

4. 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
//def

5. 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
//Java

8. 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.

Java
Programming
Spring Boot
Spring
Code
Recommended from ReadMedium