Working With Strings


Prerequisites

Strings in java, like arrays, are first-class objects. This Magercise demonstrates some common manipulations with Strings and StringBuffers.

StringBuffers are similar to Strings, but they can be modified using methods like append and prepend.

As with the array Magercise, each task can be done incrementally, by adding its code to the Scrapbook and running it after it's written.

As with arrays, some String operations can raise exceptions.

Strings are discussed in detail in the course notes.

Tasks

Perform the following tasks:

  1. Create a new String, "hello world", using a String constructor and concatenation.

  2. Print out a substring of the "hello world" string.

  3. Create another "Hello World" string using a StringBuffer and its append method. Print it to System.out.

  4. Use concatenation to convert some integers and floating point numbers to a string for printing.

  5. As in the notes, use Integer.parseInt to convert a String into an Integer, then print it to System.out.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.