More Play with substring()

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.
go to previous page   go to home page   go to next page

Answer:

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

More Play with substring()

Play with substring() using the following applet. The parameter for substring() (in this applet) must be an integer literal like 0 or 12.

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.

Try some strings other than the one suggested. Be sure to enclose the characters of the string with quote marks. (However, the applet does not support escape characters in the string.)

QUESTION 8:

Is the following correct? What string does it create?

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