«^»
3.2. Applying methods to a String object

The class java.lang.String comes with a large number of methods for manipulating strings. A list of these methods is documented in the Method Detail section of javaapi:java/lang/String.html.

For example, if you want to access an individual character of a string, you can use a method called charAt. The value that is returned is of type char. You use an argument that is an int value to indicate the position of the character which you want to be returned. However, its value needs to be one less than the position of the character. So if you want the first character of the string to be returned, you need an argument with the value 0:

String tName = new String("James Gosling");
char tFirstChar = tName.charAt(0);
System.out.println("The first character of the name is: " + tFirstChar);
The println statement will output the line:
The first character of the name is: J

There is also a method that can be used to find out how many characters there are in a string:

String tName = new String("James Gosling");
int tNameLength = tName.length();
char tLastChar = tName.charAt(tNameLength - 1);
System.out.println("The last character of the name is: " + tLastChar);
This will output:
The last character of the name is: g