14.3.7 Miscellaneous String Methods
Class String provides several methods that return Strings or character arrays containing copies of an original String’s contents which are then modified. These methods—none of which modify the String on which they’re called—are demonstrated in Fig. 14.8.
Click here to view code image
1 // Fig. 14.8: StringMiscellaneous2.java 2 // String methods replace, toLowerCase, toUpperCase, trim and toCharArray. 3
4 public class StringMiscellaneous2 5 { 6 public static void main(String[] args) 7 { 8 String s1 = "hello"; 9 String s2 = "GOODBYE";10 String s3 = " spaces ";11
12 System.out.printf("s1 = %s%ns2 = %s%ns3 = %s%n%n", s1, s2, s3);13
14 // test method ...