Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Working with String Duo Wei CS110A_004. 2 Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.

Similar presentations


Presentation on theme: "1 Working with String Duo Wei CS110A_004. 2 Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized."— Presentation transcript:

1 1 Working with String Duo Wei CS110A_004

2 2 Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized string: String s1 = ""; String s2 = new String(); Empty strings private String errorMsg; errorMsg is null

3 3 Constructors String’s no-args and copy constructors are not used much. Other constructors convert arrays into strings (used in a lab in Chapter 10). String s1 = new String (); String s2 = new String (s1); String s1 = ""; String s2 = s1;

4 4 Methods — length, charAt int length (); char charAt (k); Returns the number of characters in the string Returns the k-th char 6 ’n' ”Flower".length(); ”Wind".charAt (2); Returns: Character positions in strings are numbered starting from 0

5 5 Methods — substring String s2 = s.substring (i, k);  returns the substring of chars in positions from i to k-1 String s2 = s.substring (i);  returns the substring from the i-th char to the end ”raw" "happy" "" (empty string) ”strawberry".substring (2,5); "unhappy".substring (2); "emptiness".substring (9); Returns: strawberry i k

6 6 Methods — Concatenation String result = s1 + s2;  concatenates s1 and s2 String result = s1.concat (s2);  the same as s1 + s2 result += s3;  concatenates s3 to result result += num;  converts num to String and concatenates it to result

7 7 Methods — Find (indexOf) String date = "July 5, 2012 1:28:19 PM" ; date.indexOf ('J'); 0 date.indexOf ('2'); 8 date.indexOf ("2012"); 8 date.indexOf ('2', 9); 11 date.indexOf ("2020"); -1 date.lastIndexOf ('2'); 15 0 8 11 15 Returns: (not found) (starts searching at position 9)

8 8 Methods — Comparisons boolean b = s1.equals(s2);  returns true if the string s1 is equal to s2 boolean b = s1.equalsIgnoreCase(s2);  returns true if the string s1 matches s2, case-blind int diff = s1.compareTo(s2);  returns the “difference” s1 - s2 int diff = s1.compareToIgnoreCase(s2);  returns the “difference” s1 - s2, case-blind

9 9 Methods — Replacements String s2 = s1.trim ();  returns a new string formed from s1 by removing white space at both ends String s2 = s1.replace(oldCh, newCh);  returns a new string formed from s1 by replacing all occurrences of oldCh with newCh String s2 = s1.toUpperCase(); String s2 = s1.toLowerCase();  returns a new string formed from s1 by converting its characters to upper (lower) case

10 10 Replacements (cont’d) Example: how to convert s1 to upper case A common bug: s1 = s1.toUpperCase(); s1.toUpperCase(); s1 remains unchanged

11 11 Methods — toString It is customary to provide a toString method for your class. toString converts an object into a String (for printing it out, for debugging, etc.). is the same as System.out.print (obj); System.out.print (obj.toString());

12 12 Numbers to Strings and Strings to Numbers Integer and Double are “wrapper” classes from java.lang that represent numbers as objects. Integer and Double provide useful static methods for conversions: String s1 = Integer.toString (i); String s2 = Double.toString (d); int n = Integer.parseInt (s1); double x = Double.parseDouble (s2); int i; double d;

13 13 Numbers to Strings Three ways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num); int i; double d;


Download ppt "1 Working with String Duo Wei CS110A_004. 2 Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized."

Similar presentations


Ads by Google