What's new in Java 12: How to work with strings using the new String API
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.
- 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!
- 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!
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
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!
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!]
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!
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment