The String class l l object String class
The String class l l Ένα object της String class αναπαριστά string από characters. Το String class ανήκει στο java. lang package, το οποίο κάνει built into Java. Όπως και σε άλλες classes, String έχουν constructors και methods. Σε διαφορά με other classes, String έχουν δυο operators, τους + και += (used for concatenation). Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 3
Literal Strings l l Literal strings είναι anonymous constant objects του String class που ορίζονται σαν text in double quotes. το string text μπορεί να περιλαμβάνει “escape” characters. Για παράδειγμα: \ stands for n stands for the newline character "Biology”, "C: \jdk 1. 4\docs”, "Hellon" Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 4
Literal Strings l l (συνέχ. ) ΔΕΝ πρέπει να κατασκευαστούν: απλά υπάρχουν εκεί (they are “just there. ”) Μπορούν να ανατεθούν σε String variables. can be passed to methods and constructors as arguments. have methods you can call: String file. Name = "fish. dat"; button = new JButton("Next slide"); if (”Start". equals(cmd)). . . Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 5
Empty Strings l ένα empty string ΔΕΝ έχει characters; Είναι of length 0. String s 1 = ""; String s 2 = new String(); l Empty strings ΜΗΝ το συγχύζεται με ένα ΜΗ αρχικοποιημένο string: private String error. Msg; Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου error. Msg is null 9
Constructors l l String’s no-args and copy constructors are not used much. String s 1 = new String (); String s 1 = ""; String s 2 = new String (s 1); String s 2 = s 1; Other constructors convert arrays into strings Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 10
Methods — substring String s 2 = s. substring (i, k); – returns the substring of chars in from i to k-1 strawberry i k positions String s 2 = s. substring (i); – returns the substring from the i-th char to the end Returns: ”raw" ”strawberry". substring (2, 5); "happy" "unhappy". substring (2); "" (empty string) "emptiness". substring (9); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 12
Methods — Concatenation String result = s 1 + s 2; – concatenates s 1 and s 2 String result = s 1. concat (s 2); – the same as s 1 + s 2 result += s 3; – concatenates s 3 to result += num; – converts num to String and concatenates it to result Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 13
Methods — Find (index. Of) 0 8 11 15 String date ="July 5, 2012 1: 28: 19 PM"; date. index. Of ('J'); date. index. Of ('2'); date. index. Of ("2012"); date. index. Of ('2', 9); date. index. Of ("2020"); date. last. Index. Of ('2'); Εισαγωγή στο ΟΟΡ και Java Returns: 0 8 8 (starts searching 11 at position 9) -1 15 Δρ. Μαρία Ι. Ανδρέου (not found) 14
Methods — Comparisons boolean b = s 1. equals(s 2); – returns true if the string s 1 is equal to s 2 boolean b = s 1. equals. Ignore. Case(s 2); – returns true if the string s 1 matches s 2, caseblind int diff = s 1. compare. To(s 2); – returns the “difference” s 1 - s 2 int diff = s 1. compare. To. Ignore. Case(s 2); – returns the “difference” s 1 - s 2, case-blind Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 15
Methods — Replacements String s 2 = s 1. trim (); – returns a new string formed from s 1 by removing white space at both ends String s 2 = s 1. replace(old. Ch, new. Ch); – returns a new string formed from s 1 by replacing all occurrences of old. Ch with new. Ch String s 2 = s 1. to. Upper. Case(); String s 2 = s 1. to. Lower. Case(); – returns a new string formed from s 1 by converting its characters to upper (lower) case Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 16
Replacements (cont’d) l Example: how to convert s 1 to upper case s 1 = s 1. to. Upper. Case(); l A common bug: s 1 remains unchanged s 1. to. Upper. Case(); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 17
Methods — to. String l l It is customary to provide a to. String method for your class. to. String converts an object into a String (for printing it out, for debugging, etc. ). System. out. print (obj); is the same as System. out. print (obj. to. String()); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 18
Numbers to Strings and Strings to Numbers l l Integer and Double are “wrapper” classes from java. lang that represent numbers as objects. Integer and Double provide useful static methods for conversions: String s 1 = Integer. to. String (i); String s 2 = Double. to. String (d); int i; double d; int n = Integer. parse. Int (s 1); double x = Double. parse. Double (s 2); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 19
Numbers to Strings l Three ways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer. to. String (i); String s = Double. to. String (d); int i; double d; 3. String s = String. value. Of (num); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 20
Numbers to Strings (cont’d) l The Decimal. Format class can be used for more controlled conversions of numbers into strings: import java. text. Decimal. Format; . . . Decimal. Format money = new Decimal. Format("0. 00"); . . . double amt = …; . . . String s = money. format (amt); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 56. 7899 "56. 79" 21
Strings to Numbers int n = Integer. parse. Int(s); double x = Double. parse. Double(s); l l These methods throw a Number. Format. Exception if s does not represent a valid number. Older versions of SDK (before 1. 2) did not have Double. parse. Double; you had to use double x = Double. value. Of(s). double. Value(); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 22
Character Methods l l l java. lang. Character is a class that represents characters as objects. Character has several useful static methods that determine the type of a character. Character also has methods that convert a letter to the upper or lower case. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 23
Character Methods (cont’d) if (Character. is. Digit (ch)). . . . is. Letter. Or. Digit. . is. Upper. Case. . is. Lower. Case. . is. Whitespace. . . Whitespace is space, tab, newline, etc. – return true if ch belongs to the corresponding category Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 24
Character methods (cont’d) char ch 2 = Character. to. Upper. Case (ch 1); . to. Lower. Case (ch 1); – if ch 1 is a letter, returns its upper (lower) case; otherwise returns ch 1 int d = Character. digit (ch, radix); – returns the int value of the digit ch in the given int radix char ch = Character. for. Digit (d, radix); – returns a char that represents int d in a given int radix Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 25
String. Tokenizer l l java. util. String. Tokenizer is used to extract “tokens” from a string. Tokens are separated by delimiters (e. g. , whitespace). A tokenizer object is constructed with a given string as an argument. The second optional argument is a string that lists all delimiters (default is whitespace). Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 26
String. Tokenizer (cont’d) import java. util. String. Tokenizer; Delimiters are. . . whitespace String str = input. read. Line(); String. Tokenizer q = new String. Tokenizer (str); All delimiters // or: // new String. Tokenizer (str, "; + t, "); The number of int n = q. count. Tokens (); found tokens while ( q. has. More. Tokens() ) { String word = q. next. Token(); . . . Δρ. Μαρία Ι. Ανδρέου Εισαγωγή στο ΟΟΡ και Java 27
Equation. Solver (cont’d) l Uses many techniques discussed earlier: – trim, substring, and other String methods clean the input, surround operation signs with spaces – String. Tokenizer extracts tokens – Character. is. Digit, and other Character methods identify tokens – Integer. parse. Int converts tokens to numbers – String's + operator combines strings and numbers to generate output Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 29
Review: l l l What makes the String class unusual? How can you include a double quote character into a literal string? Is "length". length() allowed syntax? If so, what is the returned value? Define immutable objects. Does immutability of Strings make Java more efficient or less efficient? Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 30
Review (cont’d): l l l How do you declare an empty string? Why are String constructors not used very often? If the value of String city is "Boston", what is returned by city. char. At (2)? By city. substring (2, 4)? How come String doesn’t have a set. Char. At method? Is s 1 += s 2 the same as s 1 = s 1 + s 2 for strings? Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 31
Review (cont’d): l l l What do the index. Of methods do? Name a few overloaded versions. What is more efficient for strings: == and other relational operators or equals and compare. To methods? What does the trim method do? What does s. to. Upper. Case() do to s? What does the to. String method return for a String object? Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 32
Review (cont’d): l l Name a simple way to convert a number into a string. Which class has a method for converting a String into an int? Name a few Character methods that help identify the category to which a given character belongs. What is the String. Tokenizer class used for? Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου 33
- Slides: 33