String processing
In mainstream programming, String
probably is the most popular class. In Chapter 1, Getting Started with Java 17, we learned about this class, its literals, and its specific feature called string immutability. In this section, we will explain how a string can be processed using the String
class methods and utility classes from the standard library, and the StringUtils
class from the org.apache.commons.lang3
package in particular.
Methods of the String class
The String
class has more than 70 methods that enable analyzing, modifying, and comparing strings, and converting numeric literals into the corresponding string literals. To see all the methods of the String
class, please refer to the Java API online at https://docs.oracle.com/en/java/javase.
String analysis
The length()
method returns the number of characters in the string, as shown in the following code:
String s7 = "42";
System.out.println(s7.length()); //prints...