New features in Java 12: How to use the new String API for string search and replacement
Overview:
With every new version released, Java introduces some new features and Improve. In Java 12, we have a new String API update that provides a more concise and powerful way to handle string search and replace operations. In this article, we'll detail these new features and show you how to use them with code examples.
String sourceString = "This is a sample string."; String searchString = "sample"; if (sourceString.contains(searchString)) { System.out.println("String found!"); } else { System.out.println("String not found!"); }
String sourceString = "Hello, name!"; String targetString = "name"; String replacement = "John"; String resultString = sourceString.replace(targetString, replacement); System.out.println(resultString);
In the above example, we replace "name" in the source string with "John" and print the result.
String[] names = {"John", "Jane", "Tom", "Alice"}; String result = String.join(", ", names); System.out.println(result);
In the above example, we are concatenating all the elements in the string array with commas and spaces and printing the result.
The above is the introduction and examples of the new String API in Java 12 in this article. I hope that through these examples you can better understand and use these new features. If you haven't tried Java 12 yet, now is the time to use these new string manipulation methods to improve your coding efficiency.
The above is the detailed content of What's new in Java 12: How to use the new String API for string find and replace. For more information, please follow other related articles on the PHP Chinese website!