14.3.5 Extracting Substrings from Strings

Class String provides two substring methods to enable a new String object to be created by copying part of an existing String object. Each method returns a new String object. Both methods are demonstrated in Fig. 14.6.

 1   // Fig. 14.6: SubString.java 2   // String class substring methods. 3  4   public class SubString 5   { 6      public static void main(String[] args) 7      { 8         String letters = "abcdefghijklmabcdefghijklm"9 10         // test substring methods11         System.out.printf("Substring from index 20 to end is \"%s\"%n",12            letters.substring(20));13         System.out.printf("%s \"%s\"%n",14            "Substring from index 3 up to, but ...

Get Java™ How To Program (Early Objects), Tenth 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.