Strings Etc Part I Strings About Strings n
Strings, Etc. Part I: Strings
About Strings n There is a special syntax for constructing strings: "Hello" n Strings, unlike most other objects, have a defined operation (as opposed to a method): " This " + "is String " + "concatenation" 2
Useful String methods I n char. At(int index) n n boolean starts. With(String prefix) n n Returns the character at the given index position (0 -based) Tests if this String starts with the prefix String boolean ends. With(String suffix) n Tests if this String ends with the suffix String 3
Useful String methods II n boolean equals(Object obj) n n boolean equals. Ignore. Case(String other) n n Tests if this String is the same as the obj (which may be any type; false if it’s not a String) Tests if this String is equal to the other String, where case does not matter int length() n Returns the length of this string; note that this is a method, not an instance variable 4
Useful String methods III n int index. Of(char ch) n n int index. Of(char ch, int from. Index) n n Returns the position of the first occurrence of ch in this String, or -1 if it does not occur Returns the position of the first occurrence of ch, starting at (not after) the position from. Index There are two similar methods that take a String instead of a char as their first argument 5
Useful String methods IV n int last. Index. Of(char ch) n n int last. Index. Of(char ch, int from. Index) n n Returns the position of the last occurrence of ch in this String, or -1 if it does not occur Returns the position of the last occurrence of ch, searching backward starting at position from. Index There are two similar methods that take a String instead of a char as their first argument 6
Useful String methods V n String substring(int begin. Index) n n 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 begin. Index, int end. Index) n Returns a new string that is a substring of this string, beginning at the specified begin. Index and extending to the character at index end. Index - 1. Thus the length of the substring is end. Index-begin. Index 7
Useful String methods VI n String to. Upper. Case() n n String to. Lower. Case() n n Returns a new String similar to this String, in which all letters are uppercase Returns a new String similar to this String, in which all letters are lowercase String trim() n Returns a new String similar to this String, but with whitespace removed from both ends 8
Finally, a useless String method n String to. String() n n Returns this String Why do we have this method? n Consistency--Every Object has a to. String() method 9
Strings are immutable n n n A String, once created, cannot be changed None of the preceding methods modify the String, although several create a new String Statements like this create new Strings: my. String = my. String + another. Character; n n Creating a few extra Strings in a program is no big deal Creating a lot of Strings can be very costly 10
Strings, Etc. Part II: String. Buffers
About String. Buffers n n n A String. Buffer 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 String. Buffer is copied to a new location with more room String. Buffers are used to implement String concatenation n n Whenever you say String s = "ab" + "cd", Java creates a String. Buffer containing the characters a and b, appends the characters c and d to it, and converts the result back to a String As you might guess, this isn’t terribly efficient, but it’s fine if you don’t overdo it 12
String. Buffer constructors n String. Buffer() n n String. Buffer(int capacity) n n Constructs a String. Buffer with a capacity of 16 characters Constructs a String. Buffer with the requested capacity String. Buffer(String str) n Constructs a String. Buffer containing the String str 13
Useful String. Buffer methods I n String. Buffer append(X) n n Appends X to the end of this String. Buffer; also (as a convenience) returns this String. Buffer The append method is so heavily overloaded that it will work with any argument; if the argument is an object, its to. String() method is used 14
Useful String. Buffer methods II n int length() n n Returns the number of characters in this String. Buffer void set. Length(int new. Length) n Sets the number of characters in this String. Buffer; this may result in truncation of characters at the end, or addition of null characters 15
Useful String. Buffer methods III n char. At(int index) n n void set. Char. At(int index, char ch) n n Returns the character at the location index Sets the character at location index to ch String. Buffer reverse() n The sequence of characters in this String. Buffer is replaced by the reverse of this sequence, and also returned as the value of the method 16
Useful String. Buffer methods IV n String. Buffer insert(int offset, X) n n String. Buffer delete. Char. At(int index) n n Insert X starting at the location offset in this String. Buffer, and also return this String. Buffer as the value of the method. Like append, this method is heavily overloaded Deletes the character at location index String. Buffer delete(int start, int end) n Deletes chars at locations start through end-1 17
Useful String. Buffer methods V n String substring(int start) n n String substring(int start, int end) n n Returns a new String of characters from this String. Buffer, beginning with the character at the specified index and extending to the end of this string. Returns a new String of characters from this String. Buffer, beginning at location start and extending to the character at index end-1. Thus the length of the substring is end-begin String to. String() n Returns the characters of this String. Buffer as a String 18
When to use String. Buffers n n n If you make a lot (thousands) of changes or additions to a String, it is much more efficient to use a String. Buffer If you are simply examining the contents of a String, then a String is at least as efficient as a String. Buffer For incidental use (such as creating output lines), use Strings; they are more convenient 19
Strings, Etc. Part III: String. Tokenizers
About String. Tokenizers n n A String. Tokenizer is used to break a string into tokens, such as words A String. Tokenizer uses delimiters to separate tokens; a String. Tokenizer can be made that will return the delimiters, or discard them You construct a String. Tokenizer for a particular String, use it for one pass through that String, after which the String. Tokenizer is “used up” There are only a few methods for String. Tokenizers 21
String. Tokenizer constructors n String. Tokenizer(String str) n n String. Tokenizer(String str, String delim) n n Constructs a tokenizer that uses the default (whitespace) delimiters " tnrf"; it does not return delimiters as tokens Constructs a tokenizer that uses the given delimiters delim; it does not return delimiters as tokens String. Tokenizer(String str, String delim, boolean return. Delims) n Constructs a tokenizer that uses the given delimiters delim; it returns delimiters as tokens if return. Delims is true 22
String. Tokenizer methods n boolean has. More. Tokens() n n String next. Token() n n Returns the next token String next. Token(String delim) n n Tests if this tokenizer’s String has more tokens Permanently changes this tokenizer’s set of delimiters, then returns the next token int count. Tokens() n Returns the number of tokens remaining 23
Example use of String. Tokenizer n String. Tokenizer st = new String. Tokenizer("this is a test"); while (st. has. More. Tokens()) { System. out. println(st. next. Token()); } Output: this is a test 24
Strings, Etc. Part IV: Stream. Tokenizers
About Stream. Tokenizers n n A Stream. Tokenizer does the same basic things as a String. Tokenizer, but takes its input from an input stream A Stream. Tokenizer can recognize identifiers, numbers, quoted strings, and various comment styles Stream. Tokenizers are complex and powerful We won’t be doing anything with Stream. Tokenizers beyond noting their existence 26
The End 27
- Slides: 27