CIS 234: Strings
What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits, special characters (spaces, punctuation marks, mathematical operators, etc.) "Kim" "137 Flower St." "a + b"
Strings in Memory memory is assigned when a string is created if a string's value changes, its memory location (and possibly length) changes "anonymous" strings have a memory location but no variable name System.out.print("Total: "); //anonymous
Strings in Memory - 2 each character in a string can be identified by an offset from its memory location 1 st character is at address of string offset = 0 2 nd character is at string address + 1 offset = 2 bytes (Unicode) 3 rd is at address is at address + 2 x 2
String Class String is a class in Java, so strings are declared with String (1 st letter capitalized) unlike other classes, don't always have to use new String myName = new String("Ralph"); // ok String yourName = "Viji"; // also ok
String Class Methods when create a string, you create an object of the String class can use String class methods with strings you create, attaching a method to a string with a dot size = myName.length(); /*length method in String class assigns # of characters in myName object to size */
String Class Comparisons with objects, == compares memory locations, not actual values == wouldn't recognize same string value in different memory locations use String class methods to compare values
String Class equals Method compares strings in same or different memory locations aName = "Lee "; //includes spaces test = (aName.equals("Lee")) // false strings are not equal because "Lee " includes space characters
String Class compare Method compares two strings letter by letter if identical, returns 0 otherwise returns distance between 1 st nonmatching characters if string attached to method is alphabetically greater than argument, value is positive if less, value is negative
String compare Method - 2 aName = "Li"; distance = (aName.compare("Le")); // 4 // i is ASCII 105, e is ASCII 101 aName ("Li) is attached to compare method with a dot alphabetically, "Li" comes after "Lee" therefore distance is positive (+4)
String length Method size = my .length(); possible to have an array of strings length is also a method of Array class make sure that object you attach length method to is string, not an array of strings
String indexOf Method finds first location of a specific character within a string 1 st character has index of 0 if character not found, returns –1 (minus) String his = int at =
String charAt Method returns character at offset (integer) provided in argument twoName = "Lidia"; firstLetter = twoName.charAt(0); secondLetter = twoName.charAt(1);
Practice String twoName = "Lidia"; int num = twoName.length(); // num = ? int num2 = twoName.indexOf ('i'); //num2 = ? char myChar = twoName.charAt(4); //myChar = ?
startsWith, endsWith Methods compare start or end of string with another string value return true or false String aString = "vegetate"; boolean test1 = aString.startsWith("b"); boolean test2 = aString.endsWith("ate");
String replace Method replaces all occurrences of 1 character with another "global search and replace" arguments are character to find, character to replace it with String oneString = "some hot"; String twoString = oneString.replace('o', 'a'); // twoString = ?
String substring Method returns part of another string arguments are starting position (offset) and number of characters String oneString = "separate"; String twoString = oneString.substring(4, 3); // twoString = ?
String Class Case Methods toUpperCase converts to capital letters aName = aName.toUpperCase(); toLowerCase converts to lower case
Strings to Numbers use data type "wrapper" class methods wrapper classes make it possible to treat "primitive" data types like they were classes int num = Integer.parseInt("135"); Integer class is wrapper for int data type double dNum = Double.parseDouble("1.2343"); Double class is double data type wrapper