Strings String Constructors String Methods An object of the String class represents a string of characters. The String class belongs to the java.lang package, which does not require an import statement. Like other classes, String has constructors and methods. Unlike other classes, String has two operators, + and += (used for concatenation). Example: //assign a literal to a String variable String name = “Robert”; //calling a method on a literal String char firstInitial = “Robert”.charAt(0); //calling a method on a String variable char firstInitial = name.charAt(0); No argument Constructor: No-argument constructor creates an empty String. Rarely used. String empty = new String(); A more common approach is to reassign the variable to an empty literal String. (Often done to reinitialize a variable used to store input.) String empty = “”;//nothing between quotes Copy Constructors: Copy constructor creates a copy of an existing String. Not the same as an assignment. Copy Constructor: Each variable points to a different copy of the String. String word = new String(“Java”); String word2 = new String(word); word word2 “Java" “Java" Assignment: Both variables point to the same String. String word = “Java”; String word2 = word; word word2 “Java" Most other constructors take an array as a parameter to create a String. char[] letters = {‘J’, ‘a’, ‘v’, ‘a’}; String word = new String(letters);//”Java” For Example: char[ ] charArray = {‘b’, ‘i’, ‘r’, ‘t’, ‘h’, ‘ ‘, ‘d’, ‘a’, ‘y’}; String s = new String(“Hello”); String s1 = new String(); String s2 = new String(s); Empty Hello String s3 = new String (charArray); String s4 = new String(charArray, 3, 6); birth day day The String class defines a number of methods that allow us to accomplish a variety of string manipulation tasks. Some of the most commonly used string methods are discussed. length, charAt: int length(); Returns the number of characters in the string. char charAt(i); Returns the char at position i. For Example: ”Problem".length(); Character positions in strings are numbered starting from 0 – just like arrays. ”Window".charAt (2); 7 ‘n’ substring: Returns a new String by copying characters from an existing String. String subs = word.substring(i) Returns the substring from the ith character to the end. String subs = word.substring(i, k) Returns the substring of characters in position from i to k-1. For Example: ”television".substring (2, 5); “lev” “immutable".substring (2); “mutable” “bob".substring (4); error concat: String concatenation is the operation of joining two character strings end to end For example: the string “snow” and “ball” may be concatenated to give “snowball”. For Example: String word1 = “re”; String word2 = “think; String word3 = “ing”; int num = 2; String result = word1 + word2; String result1 = result.concat(word3); result1 += num; rethink rethinking rethinking2 Find: Often it’s useful to search a string for a characters. indexOf and lastIndexOf are two methods that search for a specified characters or substring in a string. For Example: String letters = “abcdefghijklmabcdefghijklm”; letters.indexOf(‘c’); 2 letters.indexOf(‘a’, 1); letters.indexOf(‘$’); 13 -1 letters.lastIndexOf(‘c’); 15 letters.lastIndexOf(‘a’, 25); 13 letters.lastindexOf(‘$’); letters.indexOf(”def”); -1 3 letters.indexOf(“def”, 7); letters.indexOf(“hello”); 16 -1 letters.lastIndexOf(“def”); letters.lastIndexOf(“def”, 25); letters.lastindexOf(“hello”); 16 16 -1 Changing Case: converts the string either to lower or upper, the methods are toLowerCase and toUpperCase. For Example: String word = “heLLo”; String word1 = word.toUpperCase(); HELLO String word2 = word.toLowerCase(); hello Trim: removes the white spaces at the beginning and end of the string. For Example: String word = “ heLLo JAVA ”; String word1 = word.trim(); heLLo Replace: replaces all the appearances of character x with y. For Example: String word = “rare” String word1 = word.replace(‘r’, ‘d’); dade JAVA Equals: Compare two strings if equal returns true if not false. The methods are equals() and equalsIgnoreCase(). It returns Boolean type. For Example: boolean b = "Raiders".equals("Raiders"); true boolean b1 = "Raiders".equals("raiders"); false boolean b2 = "Raiders".equalsIgnoreCase("raiders"); true compareTo: Compares this instance with a specified String object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified String. int diff = word1.compareTo(word2); returns the “difference” word1 - word2 int diff = word1.compareToIgnoreCase(word2); returns the “difference” word1 - word2, case-blind Usually programmers don’t care what the numerical “difference” of word1 - word2 is, just whether the difference is negative (word1 comes before word2), zero (word1 and word2 are equal) or positive (word1 comes after word2). Often used in conditional statements. For Example: int diff = “apple".compareTo(“berry"); <0 int diff1 = “Zebra".compareTo(“apple"); <0 int diff2 = “dig".compareTo(“dug"); <0 int diff3 = “dig”.compateTo(“digs”); <0 int diff4 = “apple”.compateTo(“apple”); 0 int diff5 = “dig”.compateToIgnoreCase(“dIG”); 0 int diff3 = “berry”.compateTo(“apple”); int diff3 = “BIT”.compateTo(“BIG”); int diff3 = “apple”.compateTo(“Apple”); int diff3 = “huge”.compateTo(“hug”); >0 >0 >0 >0 Numbers to Strings: converts the parameter value to string representation. Three ways to convert a number into a string 1. String s = "" + num; s = “” + 123;//”123” 2. String s = Integer.toString (i); String s = Double.toString (d); s = Integer.toString(123);//”123” s = Double.toString(3.14); //”3.14” 3. String s = String.valueOf (num); s = String.valueOf(123);//”123” 1. What is the returned value for “Rumplestiltskin”.length() 2. String city = "Bloomington“; 3. city.charAt (2)? 4. city.substring(2, 4)? 5. city.lastIndexOf(‘o’)? 6. city.indexOf(3)? 7. "sam".equals("Sam") returns ? 8. What kind of value does "sam".compareTo("Sam") return? 9. What will be stored in s? s = "mint".replace(‘t’, ‘e’); 10. What does s.toUpperCase() do to s?