Unformatted text preview:

StringsSlide 2Strings in JavaBasic String MethodsBasic Strings continuedSlide 6Searching a stringExample of string searchParsing StringsParsing Strings continuedStringBuffer classMore on StringBuffer ClassSlide 13Length v. CapacityLength v. Capacity con’tBibliographyStringsEdward J. BiebelStrings•Strings are fundamental part of all computing languages.•At the basic level, they are just a data structure that can hold a series of characters•However, strings are not implemented as a character array in Java as in other languages.Strings in Java•Strings are implemented as two classes in Java•java.lang.String provides an unchangeable String object•java.lang.StringBuffer provides a String object that can be amendedBasic String Methods•length() returns the length of the string•toLowerCase() converts the string to lower case•toUpperCase() converts the string to upper case•replace(char, char) replaces occurrences of one character with another characterBasic Strings continued•Basic strings are not meant to change frequently so there are no add or append methods•However the concat(String) method does allow two strings to be concatenated togetherBasic Strings continued•Substrings of a String object can also be accessed•A portion of String object can be copied to a character array using the getChars() method•The substring() method can return substring beginning at a specified offsetSearching a string•Methods for searching strings–indexOf(x) searches for the first occurrence of x–indexOf(x, y) searches for the first occurrence of x after the offset of y–lastIndexOf(x) searches backwards for the first occurrence of x–lastIndexOf(x, y) searches backwards for the first occurrence of x after the offset of yExample of string search •indexOf(x) and indexOf(x, y) can find all occurrences of a character(s) in a string public void paint(Graphics g) { String str = new String("Wish You Were Here"); int count = 0; int fromIndex = 0; while(fromIndex != -1) { fromIndex = str.indexOf("er", fromIndex); if (fromIndex != -1) { count++; fromIndex++; } } g.drawString(String.valueOf(count), 10, 10); }Parsing Strings•Strings can be parsed with the StringTokenizer class•The default delimiters (space, tab, newline and carriage return) can be used for parsing sentences•By specifying different delimiters, a wide variety of strings may be parsedParsing Strings continued•Different default constructors are provided–Tokenize the string based on the default delimiters–Tokenize the string based on a specified set of delimiters–Tokenize the string based on a specified set of delimiters with a boolean flag to specify whether the delimiters should also be returned as tokensStringBuffer class•The StringBuffer class is provided for strings that need may need to be changed•The StringBuffer class contains methods for both inserting and appending text•An object created as a StringBuffer can easily be converted to an object of the String class if neededMore on StringBuffer Class•Conversion may be needed because many Java library methods expect a string•The toString() method is used for converting a StringBuffer object to a String object•Example of converting a StringBuffer to a String: public void paint(Graphics g) { StringBuffer buf = new StringBuffer("Hello, World"); g.drawString(buf.toString(), 10, 10);}More on StringBuffer Class•StringBuffer objects are mutable and capacity & length affect performance•If the StringBuffer object needs to be expanded during an append or insert, a new array is created and the old data copied to it•Use capacity() and ensureCapacity(int) methods to minimize expansionsLength v. Capacity•The length() method returns the length of the string in the StringBuffer •The capacity() method returns the total “space” in a StringBuffer•The ensureCapacity(int) method insures the StringBuffer has at least the specified amount of capacity remainingLength v. Capacity con’t•Examples of length() and capacity() methodsStringBuffer buf = new StringBuffer(25);// creates StringBuffer with length 25buf.append("13 Characters"); // appends 13 charactersint len = buf.length(); // length() returns 13int cap = buf.capacity(); // capacity returns


View Full Document
Download Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?