Download presentation
Presentation is loading. Please wait.
1
CIS 234: Strings
2
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"
3
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
4
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
5
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
6
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 */
7
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
8
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
9
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
10
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)
11
String length Method size = myEmail.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
12
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 hisEmail = "mike@ice.net"; int at = hisEmail.indexOf('@');
13
String charAt Method returns character at offset (integer) provided in argument twoName = "Lidia"; firstLetter = twoName.charAt(0); secondLetter = twoName.charAt(1);
14
Practice String twoName = "Lidia"; int num = twoName.length(); // num = ? int num2 = twoName.indexOf ('i'); //num2 = ? char myChar = twoName.charAt(4); //myChar = ?
15
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");
16
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 = ?
17
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 = ?
18
String Class Case Methods toUpperCase converts to capital letters aName = aName.toUpperCase(); toLowerCase converts to lower case
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.