String and StringBuilderAbout StringsUseful String methods IUseful String methods IIUseful String methods IIIUseful String methods IVUseful String methods VUnderstanding “index”Useful String methods VIUseful String methods VIIFinally, a useless String methodStrings are immutableMore about equalsStill more about equalsStrings, Etc.About StringBuildersStringBuilder constructorsUseful StringBuilder methods IUseful StringBuilder methods IIUseful StringBuilder methods IIIUseful StringBuilder methods IVUseful StringBuilder methods VWhen to use StringBuildersStrings, etc.The Character classSome Character methodsThe EndJan 14, 2019String and StringBuilderPart I: String2About StringsStrings are objects, but there is a special syntax for writing String literals:"Hello"Strings, unlike most other objects, have a defined operation (as opposed to a method): " This " + "is String " + "concatenation""Strings can contain any character, but some of them must be “escaped” in order to write them in a literal\" stands for the double-quote (") character\n stands for the newline character\\ stands for the backslash (\)characterEach of these is written as a two-character sequence, but represents a single character in the string3Useful 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 ends10Useful String methods VIIString[] split(String regex)Breaks the string up into an array of stringsThe parameter is a regular expression that defines what separates the stringsFor example, String s = "one, two, three"; String[] ss = s.split(", ");This assigns the array {"one", "two", "three"} to ssRegular expressions are complex expressions that assign meanings to many common punctuation marks, such as +, *, period, and [Hence, regular expressions are powerful, but can be treacherous if you aren’t very familiar with them11Finally, a useless String methodString toString()Returns this StringWhy do we have this method?Consistency--Every Object has a toString() method12Strings 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 costly13More about equalsIf you write String s = "abc"; String t = "abc";the compiler only creates the string "abc" once, and makes s and t both refer to this one stringIt can do this because strings are immutableHence, the test s == t will be trueHowever, if you now write String u = "a" + "bc";the test s == u will be falseThis is because they are different stringsMoral: Use equals for strings, not ==14Still more about equalsSuppose you want to test whether a variable name has the value "Dave"Here’s the obvious way to do it: if (name.equals("Dave")) { ... }But you could also do it this way: if ("Dave".equals(name)) { ... }It turns out that the second way is usually betterWhy?If name == null, the first way will cause a NullPointerException, but the second way will just return falseJan 14, 2019Strings, Etc.Part II: StringBuilder16About StringBuildersA StringBuilder 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 StringBuilder is copied to a new location with more roomStringBuilder is a reimplementation of StringBufferThe API (collection of methods) is the sameStringBuffers are threadsafe, but StringBuilders are more efficientStringBuilders are used to implement String concatenationWhenever you say String s = "ab" +
View Full Document