Home >Java >javaTutorial >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.
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!
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:
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:
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!