MSIS 655 Advanced Business Applications Programming Week 8 Strings and Characters (Ch. 29) In this chapter you will learn: To create and manipulate immutable character string objects of class String. To create and manipulates mutable character string objects of class StringBuffer. To create and manipulate objects of class Character. To use a StringTokenizer object to break a String object into tokens. To use regular expressions to validate String data entered into an application. 11/18/2018 8.1
Introduction String and character processing String Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer String Series of characters treated as single unit May include letters, digits, special characters. Object of class String String literals 1Java treats all string literals with the same contents as a single String object that has many references to it. This conserves memory. 11/18/2018
String Constructors Fig. 29.1 demonstrates four constructors No-argument constructor One-argument constructor A String object A char array Three-argument constructor An integer specifies the starting position An integer specifies the number of characters to access 11/18/2018
String Methods (Fig. 29.2) Method length Method charAt Method getChars Determine the length of a String Like arrays, Strings always “know” their size Unlike array, Strings do not have length instance variable Method charAt Get character at specific location in String Method getChars Get entire set of characters in String Method (void) getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies characters from this string into the destination character array. ex: String s1 = “hello there”; s1.getChars( 0, 5, charArray, 0); => charArray = {“h”, “e”, “l”, “l”, “o”} 11/18/2018
Comparing Strings (Fig. 29.3) Comparing String objects Method equals Method equalsIgnoreCase Method compareTo Method regionMatches Comparing references with == can lead to logic errors, because == compares the references to determine whether they refer to the same object, not whether two objects have the same contents. When two identical (but separate) objects are compared with ==, the result will be false. When comparing objects to determine whether they have the same contents, use method equals. - Method (boolean) regionMatches (int toffset, String other, int ooffset, int len) Tests if two string regions are equal. A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent identical character sequences. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len. The result is false if and only if at least one of the following is true: toffset is negative. ooffset is negative. toffset+len is greater than the length of this String object. ooffset+len is greater than the length of the other argument. There is some nonnegative integer k less than len such that: this.charAt(toffset+k) != other.charAt(ooffset+k) 11/18/2018
Miscellaneous String Methods Methods startsWith / endsWith Methods indexOf / lastIndexOf Method substring Method concat Methods replace / toUpperCase / toLowerCase / trim Method valueOf startsWith(String prefix) returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. lastIndexOf(String str): if the string argument occurs one or more times as a substring within this object, then the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned. trim() returns a copy of the string, with leading and trailing whitespace omitted. "to".concat("get").concat("her") returns "together" 11/18/2018
Class StringBuffer Class StringBuffer When String object is created, its contents cannot change Used for creating and manipulating dynamic string data i.e., modifiable Strings Can store characters based on capacity Capacity expands dynamically to handle additional characters Uses operators + and += for String concatenation In programs that frequently perform string concatenation, or other string modifications, it is more efficient to implement the modifications with class StringBuffer (covered in Section 29.4). 11/18/2018
Class Character Treat primitive variables as objects Type wrapper classes Boolean Character Double Float Byte Short Integer Long 11/18/2018
Class StringTokenizer Partition String into individual substrings Use delimiter Typically whitespace characters (space, tab, newline, etc) Java offers java.util.StringTokenizer 11/18/2018
Lab activities (Week 8) Exercises (pp. 1392-93) 29.5, 29.7, 29.8, Advanced: 29.19 11/18/2018