Strings, Etc.About StringsUseful String methods IUseful String methods IIUseful String methods IIIUseful String methods IVUseful String methods VUnderstanding “index”Useful String methods VIFinally, a useless String methodStrings are immutableSlide 12About StringBuffersStringBuffer constructorsUseful StringBuffer methods IUseful StringBuffer methods IIUseful StringBuffer methods IIIUseful StringBuffer methods IVUseful StringBuffer methods VWhen to use StringBuffersSlide 21About StringTokenizersStringTokenizer constructorsStringTokenizer methodsExample use of StringTokenizerSlide 26About StreamTokenizersThe EndJan 14, 2019Strings, Etc.Part I: Strings2About StringsThere 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"3Useful String methods Ichar 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 Stringboolean endsWith(String suffix)Tests if this String ends with the suffix String4Useful String methods IIboolean 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 matterint length()Returns the length of this string; note that this is a method, not an instance variable5Useful String methods IIIint indexOf(char ch)Returns the position of the first occurrence of ch in this String, or -1 if it does not occurint indexOf(char ch, int fromIndex)Returns the position of the first occurrence of ch, starting at (not after) the position fromIndexThere are two similar methods that take a String instead of a char as their first argument6Useful String methods IVint lastIndexOf(char ch)Returns the position of the last occurrence of ch in this String, or -1 if it does not occurint lastIndexOf(char ch, int fromIndex)Returns the position of the last occurrence of ch, searching backward starting at position fromIndexThere are two similar methods that take a String instead of a char as their first argument7Useful String methods VString substring(int beginIndex)Returns a new string that is a substring of this string, beginning with the character at the specified index and extending to the end of this string. String substring(int beginIndex, int endIndex)Returns a new string that is a substring of this string, beginning at the specified beginIndex and extending to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex8Understanding “index”With charAt(index), indexOf(x), and lastIndexOf(x), just count characters (starting from zero)With substring(from) and substring(from, to), it works better to count positions between charactersSo, for example, substring(4, 8) is "said", andsubstring(8, 12) is ", \"H" "She said, \"Hi\"" 0 1 2 3 4 5 6 7 8 9 10 11 12 13"She said, \"Hi\""0 1 2 3 4 5 6 7 8 9 10 11 12 13 14If indexOf(',') is 8, then substring(0, indexOf(',')) is "She said"and substring(indexOf(',') + 1) is " \"Hi\""9Useful String methods VIString toUpperCase()Returns a new String similar to this String, in which all letters are uppercaseString toLowerCase()Returns a new String similar to this String, in which all letters are lowercaseString trim()Returns a new String similar to this String, but with whitespace removed from both ends10Finally, a useless String methodString toString()Returns this StringWhy do we have this method?Consistency--Every Object has a toString() method11Strings are immutableA String, once created, cannot be changedNone of the preceding methods modify the String, although several create a new StringStatements like this create new Strings: myString = myString + anotherCharacter;Creating a few extra Strings in a program is no big dealCreating a lot of Strings can be very costlyJan 14, 2019Strings, Etc.Part II: StringBuffers13About StringBuffersA 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 roomStringBuffers are used to implement String concatenationWhenever you say String s = "ab" + "cd", Java creates a StringBuffer containing the characters a and b, appends the characters c and d to it, and converts the result back to a StringAs you might guess, this isn’t terribly efficient, but it’s fine if you don’t overdo it14StringBuffer constructorsStringBuffer()Constructs a StringBuffer with a capacity of 16 charactersStringBuffer(int capacity)Constructs a StringBuffer with the requested capacityStringBuffer(String str)Constructs a StringBuffer containing the String str15Useful StringBuffer methods IStringBuffer append(X)Appends X to the end of this StringBuffer; also (as a convenience) returns this StringBufferThe append method is so heavily overloaded that it will work with any argument; if the argument is an object, its toString() method is used16Useful StringBuffer methods IIint length()Returns the number of characters in this StringBuffervoid setLength(int newLength)Sets the number of characters in this StringBuffer; this may result in truncation of characters at the end, or addition of null characters17Useful StringBuffer methods IIIchar charAt(int index)Returns the character at the location index void setCharAt(int index, char ch)Sets the character at location index to chStringBuffer reverse()The sequence of characters in this StringBuffer is replaced by the reverse of this sequence, and also returned as the value of the method18Useful StringBuffer methods IVStringBuffer insert(int offset, X)Insert X starting at the location offset in this StringBuffer, and also return this StringBuffer as the value of the method. Like append, this method is heavily overloadedStringBuffer deleteCharAt(int index)Deletes the character at location indexStringBuffer delete(int start, int end)Deletes chars at locations start through end-119Useful StringBuffer methods VString substring(int start)Returns a new String of
View Full Document