/* * Illustrates use of dot operator on String object * * Strings are immutable. Invoking a method like toUpperCase() * returns a *new* string. It does not change the original string. * */ public class StringManipulation{ public static void main(String[] args){ String phrase = new String("Today is the first day of the rest of my life!"); System.out.println(phrase); int length = phrase.length(); System.out.println("This string contains " + length + " characters"); System.out.println(); String upperCase = phrase.toUpperCase(); System.out.println(phrase); System.out.println(upperCase); System.out.println(); // Important: characters are indexed starting at 0 String substr1 = phrase.substring(10, 15); System.out.println(phrase); System.out.println(substr1); System.out.println(); // If you pass only one input to the substring() method, // it returns the substring from the specified character // to the end of the string String substr2 = phrase.substring(6); System.out.println(phrase); System.out.println("Tomorrow " + substr2); System.out.println(); String exchanged = phrase.replace('t', 'x'); System.out.println(phrase); System.out.println(exchanged); } }