Previous | Next | Trail Map | Writing Java Programs | The String and StringBuffer Classes


More Accessor Methods

For the String Class

In addition to the length() and charAt() accessors you saw on the previous page, The String class provides two accessors that return the index within the string of a specific character or string: indexOf() and lastIndexOf(). The indexOf() method searches from the beginning of the string forward, and lastIndexOf() searches from the end of the string backward.

The indexOf() and lastIndexOf() methods are frequently used in conjunction with substring() which returns a substring of the string. The following class illustrates the use of lastIndexOf() and substring() to isolate different parts of a filename.
NOTE: these methods don't do any error checking and assume that their argument contains a full directory path and a filename with an extension.

class Filename {
    String fullpath;
    char pathseparator;

    Filename(String str, char sep) {
	fullpath = str;
	pathseparator = sep;
    }

    String extension() {
	int dot = fullpath.lastIndexOf('.');
	return fullpath.substring(dot + 1);
    }

    String filename() {
	int dot = fullpath.lastIndexOf('.');
	int sep = fullpath.lastIndexOf(pathseparator);
	return fullpath.substring(sep + 1, dot);
    }

    String path() {
	int sep = fullpath.lastIndexOf(pathseparator);
	return fullpath.substring(0, sep);
    }
}
The extension() method uses lastIndexOf() to locate the last occurrence of the period ('.') in the filename. Then substring() uses the return value of lastIndexOf() to extract the filename extension--that is, the substring from the period ('.') to the end of the string. This code assumes that the filename actually has a period ('.') in it; if the filename does not have a period ('.'), then lastIndexOf() returns -1, and the substring() method throws a "string index out of range exception".

Also, notice that extension() uses dot + 1 as the argument to substring(). If the period ('.') character is the last character of the string, then dot + 1 is equal to the length of the string which is one larger than the largest index into the string (because indices start at 0). However, substring() accepts an index equal to (but not greater than) the length of the string and interpret it to mean "the end of the string".

Try this: Inspect the other methods in the Filename class and notice how the lastIndexOf() and substring() methods work together to isolate different parts of a filename.

While the methods in the example above uses only one version of the lastIndexOf() method, the String class actually supports four different versions of both the indexOf() and lastIndexOf() methods which

For the StringBuffer Class

Like String, StringBuffer provides length() and charAt() accessor methods. In addition to these two accessors, StringBuffer also has a method: capacity(). The capacity() method differs from length() in that it returns the amount of space currently allocated for the StringBuffer, rather than the amount of space used. For example, the capacity of the StringBuffer in the reverseIt() method shown here
class ReverseString {
    public static String reverseIt(String source) {
	int i, len = source.length();
	StringBuffer dest = new StringBuffer(len);

	for (i = (len - 1); i >= 0; i--) {
	    dest.append(source.charAt(i));
	}
	return dest.toString();
    }
}
never changes, while the length of the StringBuffer increases by one for each iteration of the loop.


Previous | Next | Trail Map | Writing Java Programs | The String and StringBuffer Classes