Thursday, 4 December 2014

// The "StringMethodsExample5B" class.  Page 355 (middle).
// (from the Carter book)
// Example 5B --> how to use the method "substring" in Strings.
//
// Given the following declaration:

public class StringMethodsExample5B
{
    public static void main (String[] args)
    {
             String s = "Brian Auyeung";
            // then,
            // 5B(a):
            System.out.println (s.substring (3));     
            // will return "an Auyeung" (starts from position 3)   
                                                            
            // 5B(b):
            System.out.println (s.substring (2, 5));   
            // will return "ian"   (starts at position 2 till 5)

            // 5B(c):
            System.out.println (s.substring (20));     // will "throw" a                                                                                                                                       //"StringIndexOutOfBoundsException"
                                                                                // because there is no position 20 in this String

    } // main method
} // StringMethodsExample5B class

No comments:

Post a Comment