Home > Java > javaTutorial > What's new in Java 12: How to work with strings using the new String API

What's new in Java 12: How to work with strings using the new String API

PHPz
Release: 2023-07-29 21:48:18
Original
1458 people have browsed it

New features in Java 12: How to use the new String API to process strings

Introduction:
String is one of the common data types in programming, and string processing is used in many applications are very frequent. As a popular programming language, Java is constantly updating and improving its functions. In Java 12, some new String APIs are introduced, providing more convenient and efficient string processing methods. This article will introduce these new features and demonstrate through code examples how to use the new String API for string processing.

1. New methods: transform() and indent()

The String class in Java 12 has two new methods: transform() and indent(), which can be used respectively Used to convert and indent strings.

  1. transform method:

The transform method accepts a Function parameter for converting strings. It returns a new string, which is obtained by converting the original string through the passed Function.

Code example:

String str = "Hello, world!";
String transformedStr = str.transform(s -> s.toUpperCase());
System.out.println(transformedStr); // 输出:HELLO, WORLD!
Copy after login
  1. indent method:

The indent method accepts an int parameter, which is used to add the specified number before each line of the string indent spaces. It returns a new string resulting from the original string with the specified number of indented spaces added before each line.

Code example:

String str = "Hello,
world!";
String indentedStr = str.indent(4);
System.out.println(indentedStr);
// 输出:
//     Hello,
//     world!
Copy after login

2. New method: isEmpty()

The String class in Java 12 also adds a new isEmpty() method for judgment Whether the string is empty. This method is optimized based on the old isEmpty() method, making it perform better when processing empty strings.

Code example:

String str1 = "";
String str2 = "Hello";
System.out.println(str1.isEmpty()); // 输出:true
System.out.println(str2.isEmpty()); // 输出:false
Copy after login

3. New method: indentLines()

The String class in Java 12 also adds a new indentLines() method, which is used to Each line of the string is indented.

Code example:

String str = "Hello,
world!";
String indentedLinesStr = str.indentLines(4);
System.out.println(indentedLinesStr);
// 输出:
//     Hello,
//     world!
Copy after login

4. New method: lines()

The String class in Java 12 has a new lines() method, which is used to convert characters Split the string into a stream consisting of each line.

Code example:

String str = "Hello,
world!";
List<String> lines = str.lines().collect(Collectors.toList());
System.out.println(lines);
// 输出:[Hello, world!]
Copy after login

5. New methods: strip(), stripLeading() and stripTrailing()

The String class in Java 12 adds strip( ), stripLeading() and stripTrailing() are used to remove spaces in strings. These methods are optimized based on the old trim() method, making the processing more efficient.

Code sample:

String str = "   Hello, world!   ";
System.out.println(str.strip()); // 输出:Hello, world!
System.out.println(str.stripLeading()); // 输出:Hello, world!   
System.out.println(str.stripTrailing()); // 输出:   Hello, world!
Copy after login

6. Conclusion

The new String API in Java 12 provides a more convenient and efficient string processing method, bringing developers Comes convenience. Through the introduction and code examples of this article, readers can learn how to use these new String methods and make full use of them in actual development to improve the efficiency and readability of the code.

To summarize, in Java 12, we have learned the following new String API:

  • The transform() and indent() methods are used to convert and indent strings Processing;
  • isEmpty() method is used to determine whether the string is empty;
  • indentLines() method indents each line of the string;
  • lines( ) method splits the string into a stream of each line; the
  • strip(), stripLeading() and stripTrailing() methods are used to remove spaces in the string.

These new String APIs can greatly facilitate our processing of strings and improve the efficiency and readability of the code. In actual development, we should make full use of these new features and continuously optimize our code.

Reference source:

  • https://openjdk.java.net/jeps/325
  • https://www.baeldung.com/java-12 -new-string-methods

The above is the detailed content of What's new in Java 12: How to work with strings using the new String API. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template