Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 201 Lecture 6: John Hurley Cal State LA Character Codes Computers store information in bits, not as characters Numeric values are easy to map to bits; just convert them to base 2 and then map each digit to a bit There are several code systems to assign numeric values to characters Character Codes ASCII developed in the early 1960s, when memory was much more expensive than it is now. Originally used to send teletype data over phone lines, which are noisy 128 (27) possible characters The first 33 are control characters, originally used to control teletypes Character Codes Unicode Used in Java Superset of ASCII 65536 values The first 128 are the same as ASCII Adds other alphabets, mathematical symbols,etc. Unicode and Hardware Memory address Memory content . . . . . . 2000 01001010 Encoding for character ‘J’ 2001 01100001 Encoding for character ‘a’ 2002 01110110 Encoding for character ‘v’ 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 Character Codes public class ASCIIUnicodeDemo{ public static void main(String[] args){ for(int charNum = 0; charNum < 128; charNum++){ System.out.println("Character " + charNum + " is " + (char) charNum); } } } Why do we get a blank line after 10 and before 11? Strings A String consists of zero or more characters String is a class that contains many methods Strings are more complicated than they sound! Constructing Strings String newString = new String(stringLiteral); String message = new String("Welcome to Java"); Since strings are used frequently, Java provides a shorthand initializer for creating a string: String message = "Welcome to Java"; 8 Variables vs Objects Consider this code: int myInt = 1; 1 is a value myInt is a variable String s1 = "The moon is made of green cheese." "The moon is made of green cheese" is a String, an object of class String. s1 is a variable 9 Null A null value is one that does not exist. Variables whose data type is a class, like String, may exist without having any value: String s = null; A variable whose type is a primitive data type will be null if no value is set, but can't be set to null int x; // ok int x = null; //syntax error 10 Strings Are Immutable A String object is immutable; its contents cannot be changed. The following code does not change the contents of the String, it makes the variable s point to a different String in memory. String s = "Java"; s = "HTML"; 11 animation Trace Code String s = "Java"; s = "HTML"; After executing s = "HTML"; After executing String s = "Java"; s : String String object for "Java" Contents cannot be changed s : String String object for "Java" : String String object for "HTML" 12 This string object is now unreferenced animation Trace Code String s = "Java"; s = "HTML"; After executing s = "HTML"; After executing String s = "Java"; s : String String object for "Java" Contents cannot be changed s : String String object for "Java" : String String object for "HTML" 13 This string object is now unreferenced Interned Strings Since strings are immutable, if two different variables need to point to identical Strings, there is no need to save two separate Strings in memory. The JVM usually uses a unique instance for each set of string literals with the same character sequence. In other words, the following code sets up one String in memory, but two variables that point to it: String s1 = "John"; String s2 = "John"; Such an instance is said to be interned. If you set up a new String using the full new String syntax, however, the JVM will not use an interned String even if there is one with identical text. This code sets up two separate Strings with identical characters: String s1 = "John"' String s2 = new String("John"); 14 Examples String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); s1 s3 : String Interned string object for "Welcome to Java" String s3 = "Welcome to Java"; System.out.println("s1 == s2 is " + (s1 == s2)); s2 System.out.println("s1 == s3 is " + (s1 == s3)); : String A string object for "Welcome to Java" s1 == s2 is false, because the == operation for Strings tests whether two Strings are the same object in memory, and we did not use the interned String s1 == s3 is true because without the new String() syntax, we used the interned String. Thus, s1 and s3 point to the same object 15 in memory. animation Trace Code String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java"; 16 s1 : String Interned string object for "Welcome to Java" Trace Code String s1 = "Welcome to Java"; s1 String s2 = new String("Welcome to Java"); : String Interned string object for "Welcome to Java" String s3 = "Welcome to Java"; s2 : String A string object for "Welcome to Java" 17 Trace Code String s1 = "Welcome to Java"; s1 s3 String s2 = new String("Welcome to Java"); : String Interned string object for "Welcome to Java" String s3 = "Welcome to Java"; s2 : String A string object for "Welcome to Java" 18 String Comparisons .equals(String otherString) test whether the text of this String is the same as the text of the other String java.lang.String +equals(s1: String): boolean Returns true if this string is equal to string s1. +equalsIgnoreCase(s1: String): boolean Returns true if this string is equal to string s1 caseinsensitive. +compareTo(s1: String): int Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or less than s1. +compareToIgnoreCase(s1: String): int Same as compareTo except that the comparison is caseinsensitive. +regionMatches(toffset: int, s1: String, offset: int, len: int): boolean Returns true if the specified subregion of this string exactly matches the specified subregion in string s1. +regionMatches(ignoreCase: boolean, toffset: int, s1: String, offset: int, len: int): boolean Same as the preceding method except that you can specify whether the match is case-sensitive. +startsWith(prefix: String): boolean Returns true if this string starts with the specified prefix. +endsWith(suffix: String): boolean Returns true if this string ends with the specified suffix. 19 String Comparisons String .equals() method if (s1.equals(s2)){ // executed if s1 and s2 have the same contents } if (s1 == s2) { // executed if s1 and s2 have the same memory reference } 20 String Comparisons, cont. compareTo(Object object) String s1 = new String("Welcome"); String s2 = "welcome"; if (s1.compareTo(s2) > 0) { // s1 is greater than s2 } else if (s1.compareTo(s2) == 0) { // s1 and s2 have the same contents } else // s1 is less than s2 21 public class StringDemo{ public static void main(String[] args){ String s1 = new String("Welcome"); String s2 = "welcome"; String s3 = s1; String s4 = new String("Welcome"); if (s1.equals(s2)){ System.out.println("s1 equals s2"); } else System.out.println("s1 does not equal s2"); if (s1 == s2) { System.out.println("s1 == s2"); } else System.out.println("s1 != s2"); if (s1.equals(s3)){ System.out.println("s1 equals s3"); } if (s1 == s3) { System.out.println("s1 == s3"); } else System.out.println("s1 != s3"); if (s1.equals(s4)){ System.out.println("s1 equals s4"); } else System.out.println("s1 does not equal s4"); if (s1 == s4){ System.out.println("s1 == s4"); } else System.out.println("s1 != s4"); if (s1.compareTo(s2) < 0) { System.out.println("s1 < s2"); } } } Escape Sequences • Some characters will cause confusion in output • What will happen if we try to execute this: •System.out.println(""Hi,Mom""); • Others, like backspace and tab, can’t be unambiguously typed •Use escape characters for these cases 23 Escape Sequences Description Escape Sequence Backspace \b Tab \t Linefeed \n Carriage return \r (think of a typewriter) Backslash \\ Single Quote \' Double Quote \" 24 String Concatenation Appending a String or character to the end of another String is called concatenation. Three strings concatenated: String message = "Welcome " + "to " + "Java"; String s1 concatenated with character B: String s1 = "Supplement" + 'B'; // s1 becomes SupplementB To concatenate to existing String, create a new String and use concat(): String myString = "Good"; String myOtherString = myString.concat(" Morning"); 25 String Concatenation String s3 = s1.concat(s2); String s3 = s1 + s2; s1 + s2 + s3 + s4 + s5 same as (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5); 26 String Length, Characters, and Combining Strings java.lang.String +length(): int Returns the number of characters in this string. +charAt(index: int): char Returns the character at the specified index from this string. +concat(s1: String): String Returns a new string that concatenate this string with string s1. string. 27 Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7) 28 Retrieving Individual Characters in a String Do not use message[0] Use message.charAt(index) Index starts from 0 Indices 0 1 2 3 4 5 6 message W e l c o m e message.charAt(0) 29 7 8 9 t o message.length() is 15 10 11 12 13 14 J a v a message.charAt(14) Strings public static void main(String[] args){ String s1 = new String("Welcome"); String s2 = " Shriners"; String s3 = s1.concat(s2); System.out.println(s3); String s4 = s1 + s2; System.out.println(s4); s1 = s1 + s2; System.out.println(s1); if(s1.startsWith("Wel")) System.out.println("\"" + s1 + "\"" + " starts with \"Wel!\""); System.out.println("Length of \"" + s1 + "\" is " + s1.length()); System.out.println("In the string \"" + s1 + "\", the character at position 4 is " + s1.charAt(4)); } Extracting Substrings You can extract a single character from a string using the charAt method.You can also extract a substring from a string using the substring method in the String class. String s1 = "Welcome to Java"; String s2 = s1.substring(0, 11) + "HTML"; Indices 0 1 2 3 4 5 6 message W e l c o m e 7 8 9 t o message.substring(0, 11) 31 10 11 12 13 14 J a v a message.substring(11) Converting, Replacing, and Splitting Strings java.lang.String +toLowerCase(): String Returns a new string with all characters converted to lowercase. +toUpperCase(): String Returns a new string with all characters converted to uppercase. +trim(): String Returns a new string with blank characters trimmed on both sides. +replace(oldChar: char, newChar: char): String Returns a new string that replaces all matching character in this string with the new character. +replaceFirst(oldString: String, Returns a new string that replaces the first matching substring in newString: String): String this string with the new substring. +replaceAll(oldString: String, Returns a new string that replace all matching substrings in this newString: String): String string with the new substring. +split(delimiter: String): Returns an array of strings consisting of the substrings split by the String[] delimiter. 32 Examples "Welcome".toLowerCase() returns a new string, welcome. "Welcome".toUpperCase() returns a new string, WELCOME. " Welcome ".trim() returns a new string, Welcome. "Welcome".replace('e', 'A') returns a new string, WAlcomA. "Welcome".replaceFirst("e", "AB") returns a new string, WABlcome. "Welcome".replace("e", "AB") returns a new string, WABlcomAB. "Welcome".replace("el", "AB") returns a new string, WABlcome. 33 Finding a Character or a Substring in a String java.lang.String +indexOf(ch: char): int Returns the index of the first occurrence of ch in the string. Returns -1 if not matched. +indexOf(ch: char, fromIndex: int): int Returns the index of the first occurrence of ch after fromIndex in the string. Returns -1 if not matched. +indexOf(s: String): int Returns the index of the first occurrence of string s in this string. Returns -1 if not matched. +indexOf(s: String, fromIndex: Returns the index of the first occurrence of string s in this string int): int after fromIndex. Returns -1 if not matched. +lastIndexOf(ch: int): int Returns the index of the last occurrence of ch in the string. Returns -1 if not matched. +lastIndexOf(ch: int, Returns the index of the last occurrence of ch before fromIndex fromIndex: int): int in this string. Returns -1 if not matched. +lastIndexOf(s: String): int Returns the index of the last occurrence of string s. Returns -1 if not matched. +lastIndexOf(s: String, Returns the index of the last occurrence of string s before fromIndex: int): int fromIndex. Returns -1 if not matched. 34 Finding a Character or a Substring in a String "Welcome "Welcome "Welcome "Welcome "Welcome "Welcome "Welcome 35 to to to to to to to Java".indexOf('W') returns 0. Java".indexOf('x') returns -1. Java".indexOf('o', 5) returns 9. Java".indexOf("come") returns 3. Java".indexOf("Java", 5) returns 11. Java".indexOf("java", 5) returns -1. Java".lastIndexOf('a') returns 14. StringBuilder The StringBuilder class is a supplement to the String class. StringBuilder is more flexible than String.You can add, insert, or append new contents into a string buffer, whereas the value of a String object is fixed once the string is created. You will often have to parse the StringBuilder to a String when you are done constructing it. 36 StringBuilder Constructors java.lang.StringBuilder +StringBuilder() Constructs an empty string builder with capacity 16. +StringBuilder(capacity: int) Constructs a string builder with the specified capacity. +StringBuilder(s: String) Constructs a string builder with the specified string. 37 Modifying Strings in the Builder java.lang.StringBuilder +append(data: char[]): StringBuilder Appends a char array into this string builder. +append(data: char[], offset: int, len: int): StringBuilder Appends a subarray in data into this string builder. +append(v: aPrimitiveType): StringBuilder Appends a primitive type value as a string to this builder. +append(s: String): StringBuilder Appends a string to this string builder. +delete(startIndex: int, endIndex: int): StringBuilder Deletes characters from startIndex to endIndex. +deleteCharAt(index: int): StringBuilder Deletes a character at the specified index. +insert(index: int, data: char[], offset: int, len: int): StringBuilder Inserts a subarray of the data in the array to the builder at the specified index. +insert(offset: int, data: char[]): StringBuilder Inserts data into this builder at the position offset. +insert(offset: int, b: aPrimitiveType): StringBuilder Inserts a value converted to a string into this builder. +insert(offset: int, s: String): StringBuilder Inserts a string into this builder at the position offset. +replace(startIndex: int, endIndex: int, s: String): StringBuilder Replaces the characters in this builder from startIndex to endIndex with the specified string. +reverse(): StringBuilder Reverses the characters in the builder. +setCharAt(index: int, ch: char): void Sets a new character at the specified index in this builder. 38 The toString, capacity, length, setLength, and charAt Methods java.lang.StringBuilder +toString(): String Returns a string object from the string builder. +capacity(): int Returns the capacity of this string builder. +charAt(index: int): char Returns the character at the specified index. +length(): int Returns the number of characters in this builder. +setLength(newLength: int): void Sets a new length in this builder. +substring(startIndex: int): String Returns a substring starting at startIndex. +substring(startIndex: int, endIndex: int): String Returns a substring from startIndex to endIndex-1. +trimToSize(): void Reduces the storage size used for the string builder. 39 Examples public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder("Welcome to "); System.out.println(stringBuilder); stringBuilder.append("Java"); System.out.println(stringBuilder); stringBuilder.insert(11, "HTML and "); System.out.println(stringBuilder); stringBuilder.delete(8, 11); // changes the builder to Welcome HTML and Java. System.out.println(stringBuilder); stringBuilder.deleteCharAt(8); // changes the builder to Welcome TML and Java. System.out.println(stringBuilder); stringBuilder.reverse(); // changes the builder to avaJ dna LMT emocleW System.out.println(stringBuilder); stringBuilder.reverse(); // undoes the reverse() System.out.println(stringBuilder); stringBuilder.replace(11, 15, " to"); // changes the builder to Welcome TML to Java. System.out.println(stringBuilder); stringBuilder.delete(7,11); // back to Welcome to Java System.out.println(stringBuilder); stringBuilder.setCharAt(0, 'w'); // sets the builder to welcome to Java. System.out.println(stringBuilder); } 40 StringBuilder public class StringBuilderDemo{ public static void main(String[] Args){ StringBuilder sb = new StringBuilder("Bakey"); System.out.println(sb); sb.insert(4, "ry"); System.out.println(sb); sb.deleteCharAt(5); System.out.println(sb); // this will not do what you expect! sb.append(" " + sb.reverse()); System.out.println(sb); sb.delete(sb.indexOf(" "), sb.length()); System.out.println(sb); StringBuilder sb2 = new StringBuilder(sb); sb.deleteCharAt(5); sb2.reverse(); sb.append(sb2); System.out.println(sb); sb.insert(5," "); System.out.println(sb); sb.replace(0,0,"Y"); System.out.println(sb); sb.deleteCharAt(1); System.out.println(sb); sb.reverse(); System.out.println(sb); } }