«^»
3.7. A program that uses these ideas about Strings

Suppose we want a program that takes a person's name arranged as FirstName Surname and outputs it in the format Surname, Initial where Initial is the first letter of the FirstName. We will also suppose that the output must be displayed in upper-case. Here is a program that does this for the name "James Gosling":

0216: public class SimpleString {                             // SimpleString.java
0217:    public static void main(final String[] pArgs) {
0218:       final String tName = new String("James Gosling");
0219:       System.out.println(tName);
0220:       final char tFirstChar = tName.charAt(0);
0221:       final int tPositionOfSpace = tName.indexOf(" ");
0222:       final String tSurname = tName.substring(tPositionOfSpace + 1);
0223:       String tLabel = tSurname + ", " + tFirstChar;
0224:       tLabel = tLabel.toUpperCase();
0225:       System.out.println(tLabel);
0226:    }
0227: }

The two printlns of this program produce the following output:

James Gosling
GOSLING, J