String Manipulation

Although the String class is used for string constants, the class does provide some string manipulation methods that "modify" the String object. The word modify is in quotation marks here because as stated earlier, these string manipulation methods don't actually change the original String object, but rather create an additional String object that incorporates the requested changes. A good example of this is the replace() method, which enables you to replace any character in a string with another character:

String str1 = "THIS IS A STRING";
String str2 = str1.replace('T', 'X');

If the code fragment above were to be executed, str2 would contain the String XHIS IS A SXRING after execution because the call to replace requests ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.