Strings, Etc.About StringsUseful String methods IUseful String methods IIUseful String methods IIIUseful String methods IVUseful String methods VUseful String methods VIFinally, a useless String methodStrings are immutableSlide 11About StringBuffersStringBuffer constructorsUseful StringBuffer methods IUseful StringBuffer methods IIUseful StringBuffer methods IIIUseful StringBuffer methods IVUseful StringBuffer methods VWhen to use StringBuffersSlide 20About StringTokenizersStringTokenizer constructorsStringTokenizer methodsExample use of StringTokenizerSlide 25About StreamTokenizersThe EndStrings, Etc.Part I: StringsAbout Strings•There is a special syntax for constructing strings: "Hello"•Strings, unlike most other objects, have a defined operation (as opposed to a method): " This " + "is String " + "concatenation"Useful String methods I•char charAt(int index)–Returns the character at the given index position (0-based)•boolean startsWith(String prefix)–Tests if this String starts with the prefix String•boolean endsWith(String suffix)–Tests if this String ends with the suffix StringUseful String methods II•boolean equals(Object obj)–Tests if this String is the same as the obj (which may be any type; false if it’s not a String)•boolean equalsIgnoreCase(String other)–Tests if this String is equal to the other String, where case does not matter•int length()–Returns the length of this string–This is a method, not an instance variableUseful String methods III•int indexOf(char ch)–Returns the position of the first occurrence of ch in this String, or -1 if it does not occur•int indexOf(char ch, int fromIndex)–Returns the position of the first occurrence of ch, starting at (not after) the position fromIndex•There are corresponding methods that take a String instead of a char as their first argument–int indexOf(String s)–int indexOf(String s, int fromIndex)Useful String methods IV•int lastIndexOf(char ch)–Returns the position of the last occurrence of ch in this String, or -1 if it does not occur•int lastIndexOf(char ch, int fromIndex)–Returns the position of the last occurrence of ch, searching backward starting at position fromIndex•There are corresponding methods that take a String instead of a char as their first argumentUseful String methods V•String substring(int beginIndex)–Returns a new string that is a substring of this string, starting at beginIndex and ending at the end of this string•String substring(int beginIndex, int endIndex)–Returns a new string that is a substring of this string, starting at beginIndex and ending at index endIndex - 1–The length of the substring is endIndex-beginIndexUseful String methods VI•String toUpperCase()–Returns a new String similar to this String, in which all letters are uppercase•String toLowerCase()–Returns a new String similar to this String, in which all letters are lowercase•String trim()–Returns a new String similar to this String, but with whitespace removed from both endsFinally, a useless String method•String toString()–Returns this String•Why do we have this method?–Consistency--Every Object has a toString() methodStrings are immutable•A String, once created, cannot be changed•None of the preceding methods modify the String, although several create a new String•Statements like this create new Strings: myString = myString + anotherCharacter;•Creating a few extra Strings in a program is no big deal•Creating a lot of Strings can be very costlyStrings, Etc.Part II: StringBuffersAbout StringBuffers•A StringBuffer has a capacity (the number of characters it can hold) and a length (the number of characters it is currently holding)•If the capacity is exceeded, the StringBuffer is copied to a new location with more room•StringBuffers are used to implement String concatenationStringBuffer constructors•StringBuffer()–Constructs a StringBuffer with a capacity of 16 characters•StringBuffer(int capacity)–Constructs a StringBuffer with the requested capacity•StringBuffer(String str)–Constructs a StringBuffer containing the String strUseful StringBuffer methods I•StringBuffer append(X)–Appends X to the end of this StringBuffer–Also (as a convenience) returns this StringBuffer•The append method is so heavily overloaded that it will work with any argument–If the argument is an object, its toString() method is used to obtain the String to appendUseful StringBuffer methods II•int length()–Returns the number of characters in this StringBuffer•void setLength(int newLength)–Sets the number of characters in this StringBuffer–May result in truncation of characters at the end, or addition of null charactersUseful StringBuffer methods III•char charAt(int index)–Returns the character at the location index •void setCharAt(int index, char ch)–Sets the character at location index to ch•StringBuffer reverse()–The sequence of characters in this StringBuffer is replaced by the reverse of this sequence, and also returned as the value of the methodUseful StringBuffer methods IV•StringBuffer insert(int offset, X)–Insert X starting at the location offset in this StringBuffer–Also returns this (modified) StringBuffer as the value of the method–Like append, this method is heavily overloaded•StringBuffer deleteCharAt(int index)–Deletes the character at location index•StringBuffer delete(int start, int end)–Deletes chars at locations start through end-1Useful StringBuffer methods V•String substring(int start)–Returns a new String of characters from this StringBuffer, from location start up through the end of the StringBuffer •String substring(int start, int end)–Returns a new String of characters from this StringBuffer, from location start up to and including the character at index end-1–The length of the substring is end-begin•String toString()–Returns the characters of this StringBuffer as a StringWhen to use StringBuffers•If you make a lot of changes or additions to a String, it is much more efficient to use a StringBuffer•If you are simply examining the contents of a String, then a String is at least as efficient as a StringBuffer•For incidental use (such as creating output lines), use Strings; they are more convenientStrings, Etc.Part III: StringTokenizersAbout StringTokenizers•A StringTokenizer is used to break a string into tokens, such as words•A StringTokenizer uses delimiters to
View Full Document