More about Strings

This chapter accompanies the previous one. Read it for even more info on Strings.

8. More Play with substring()


Line of code:
New String
String line = "buttercup" ;
String a = line.substring( 0, 6 );
String b = line.substring( 6, 9 );
String c = line.substring( 0, 1 );
String d = line.substring( 0, 0 );
String e = line.substring( 5, 5 );
String f = line.substring( 4, line.length()+1 );
String g = line.substring( 6, 3 );

More Play with substring()

Play with substring() with the following demonstration. The parameters for substring() must be integer literals like 0 or 12.

Try some other strings than the one suggested. Be sure to enclose the characters of the string with quote marks. (However, the demonstration does not support escape characters in the string, so don't try tab characters.)

The resulting string may sometimes be the empty string or contain spaces. The quote marks in the result are not part of the string that the substring method produces.

If the demonstration does not work on your computer, Javascript might not be enabled. Just skip this page.

String line =  ; // Enter a string here between quotes

String result = line.substring(  , ) ; // Enter integer parameters from and to


Question 8:

Is the following correct? What string does it create?

    String str = "buttercup" ; str = str.substring(3).substring(1,4);