Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.

Similar presentations


Presentation on theme: "Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer."— Presentation transcript:

1 Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer

2 Fundamentals of Characters and Strings Characters “Building blocks” of Java source programs String Series of characters treated as single unit May include letters, digits, etc. Object of class String See API http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

3 String Constructors Class String provides eleven constructors String() ▴ Initializes a newly created String object so that it represents an empty character sequence. String(byte[] bytes) ▴ Constructs a new String by decoding the specified array of bytes using the platform's default charset. String(byte[] bytes, int offset, int length) ▴ Constructs a new String by decoding the specified subarray of bytes using the platform's default charset. String(byte[] bytes, int offset, int length, String charsetName) ▴ Constructs a new String by decoding the specified subarray of bytes using the specified charset. String(byte[] bytes, String charsetName) ▴ Constructs a new String by decoding the specified array of bytes using the specified charset.

4 String Constructors String(char[] value) ▴ Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. String(char[] value, int offset, int count) ▴ Allocates a new String that contains characters from a subarray of the character array argument. String(int[] codePoints, int offset, int count) ▴ Allocates a new String that contains characters from a subarray of the Unicode code point array argument. String(String original) ▴ Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. String(StringBuffer buffer) ▴ Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. String(StringBuilder builder) ▴ Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

5 String Constructors See example in Figure 29.1 Uses 4 different constructors Note how jGRASP denotes literal Strings

6 String Methods length, charAt and getChars Method length Determine String length ▴ Like arrays, String s always “know” their size ▴ Unlike array, String s do not have length instance variable Method charAt Get character at specific location in String Method getChars Get entire set of characters in String See example in Figure 29.2

7 Comparing Strings Comparing String objects Method equals Method equalsIgnoreCase Method compareTo Method regionMatches See example in Figure 29.3 == compares two Strings to see if they refer to the same object.equals() compares to Strings to see if they contain the same value

8 Locating Characters and Substrings in Strings See examples in Figures 29.4 and 29.5

9 Extracting Substrings from Strings Create String s from other String s Method substring See example in Figure 29.6

10 Concatenating Strings + is a concatenation operator for Strings, although it is implemented using a StringBuffer Method concat concatenates two String objects See example in Figure 29.7

11 Miscellaneous String Methods Miscellaneous String methods Return modified copies of String Return character array See example in Figure 29.8

12 String Method valueOf String provides static class methods Method valueOf ▴ Returns String representation of object, data, etc. See example in Figure 29.9

13 Class StringBuffer When String object is created, its contents cannot change Used for creating and manipulating dynamic string data ▴ i.e., modifiable String s Can store characters based on capacity ▴ Capacity expands dynamically to handle additional characters ▴ length is always <= capacity Uses operators + and += for String concatenation See API http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

14 StringBuffer Constructors Four StringBuffer constructors StringBuffer() ▴ Constructs a string buffer with no characters in it and an initial capacity of 16 characters. StringBuffer(CharSequence seq) ▴ Constructs a string buffer that contains the same characters as the specified CharSequence. StringBuffer(int capacity) ▴ Constructs a string buffer with no characters in it and the specified initial capacity. StringBuffer(String str) ▴ Constructs a string buffer initialized to the contents of the specified string. See example in Figure 29.10

15 StringBuffer Methods length, capacity, setLength and ensureCapacity Method length Return StringBuffer length Method capacity Return StringBuffer capacity Method setLength Increase or decrease StringBuffer length Method ensureCapacity Set StringBuffer capacity Guarantee that StringBuffer has minimum capacity See example in Figure 29.11

16 StringBuffer Methods charAt, setCharAt, getChars and reverse Manipulating StringBuffer characters Method charAt ▴ Return StringBuffer character at specified index Method setCharAt ▴ Set StringBuffer character at specified index Method getChars ▴ Return character array from StringBuffer Method reverse ▴ Reverse StringBuffer contents See example in Figure 29.12

17 StringBuffer append Methods Method append Allow data values to be added to StringBuffer See example in Figure 29.13

18 StringBuffer Insertion and Deletion Methods Method insert Allow data-type values to be inserted into StringBuffer Methods delete and deleteCharAt Allow characters to be removed from StringBuffer See example in Figure 29.14

19 Wrapper Class Character Treat primitive variables as objects Type wrapper classes Boolean Character Double Float Byte Short Integer Long See examples in Figures 29.15, 29.16, and 29.17

20 Class StringTokenizer Tokenizer Partition String into individual substrings Use delimiter Java offers java.util.StringTokenizer See example in Figure 29.18

21 Regular Expressions Sequence of characters and symbols Defines a set of possible inputs, generalizes a wildcard Predefined set of strings \d – any digit \D – any non-digit \w – any word character (letter, digit, underscore) \W – any non-word character \s – any whitespace character (space, tab, CR, LF, FF) \S – any non-whitespace character

22 Example Regular Expressions Square brackets can be used to match any enclosed character A range of characters can be specified using - The complement can be created by using ^ as the first character Examples [aeiou] will match any vowel [a-zA-Z] will match any letter [^aeiou] will match any non-vowel character

23 Example Regular Expressions * matches zero or more patterns + matches one or more patterns ? matches zero or one patterns {n} matches exactly n patterns {n,} matches up to n patterns {n,m} matches between m and n patterns () can be used to group specifications | is an or for patterns Examples A+ and A* will match AAA [0-9]A* will match 1, [0-9]A+ will not; both match 1AAA

24 Class Pattern and Class Matcher Class Pattern An immutable regular expression Class Match A regular expression matching operation See exmaples in Figures 29.20, 29.21, 29.23, 29.24


Download ppt "Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer."

Similar presentations


Ads by Google