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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
