The String and StringBuffer Classes |
For the String Class
In addition to thelength()
andcharAt()
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()
andlastIndexOf()
. TheindexOf()
method searches from the beginning of the string forward, andlastIndexOf()
searches from the end of the string backward.The
indexOf()
andlastIndexOf()
methods are frequently used in conjunction withsubstring()
which returns a substring of the string. The following class illustrates the use oflastIndexOf()
andsubstring()
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.Theclass 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); } }extension()
method useslastIndexOf()
to locate the last occurrence of the period ('.') in the filename. Thensubstring()
uses the return value oflastIndexOf()
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 ('.'), thenlastIndexOf()
returns -1, and thesubstring()
method throws a "string index out of range exception".Also, notice that
extension()
usesdot + 1
as the argument tosubstring()
. If the period ('.') character is the last character of the string, thendot + 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()
andsubstring()
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 theindexOf()
andlastIndexOf()
methods which
- returns the index of the first (last) occurrence of the specified character
- returns the index of the first (last) occurrence of the specified character, searching forward (backward) from the specified index
- returns the index of the first (last) occurrence of the specified String.
- returns the index of the first (last) occurrence of the specified String, searching forward (backward) from the specified index
For the StringBuffer Class
Like String, StringBuffer provideslength()
andcharAt()
accessor methods. In addition to these two accessors, StringBuffer also has a method:capacity()
. Thecapacity()
method differs fromlength()
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 thereverseIt()
method shown herenever changes, while the length of the StringBuffer increases by one for each iteration of the loop.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(); } }
The String and StringBuffer Classes |