Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Chapter 7 Strings and Characters.

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Chapter 7 Strings and Characters."— Presentation transcript:

1 ©2004 Brooks/Cole Chapter 7 Strings and Characters

2 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A String is a Sequence of Characters Literal strings are enclosed in double quotes –"hello" Strings are objects that belong to the String class Each character in the string has an index –first character has index 0

3 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Strings are Objects Variables which refer to Strings are reference variables –Variable memory stores the address of the String object –Data (character sequence) is stored in a different part of memory

4 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating Strings Declaring a String Variable String message; A literal string is created automatically. –Assignment stores the address in a variable. message = "hello"; A String object can be created with new This is known as instantiation message = new String("hello"); The String class has several constructors (overloaded)

5 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Creation String string1 = new String("Hello"); String string2 = new String("Hello there");

6 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Concatenation We can join two strings into a larger string using the + operator String s1 = "Hot "; String s2 = "Dog"; String s3 = s1 + s2;

7 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Concatenation creates a new String String message = "Start"; message = message + "le";

8 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Strings are immutable Once created, a String object cannot be modified –String is created with a certain amount of memory All the String methods that appear to modify a String actually return a new String There is a StringBuffer class that is mutable

9 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String I/O We have used print and println to output Strings to the console We use the next and nextLine methods of the Scanner class to read String data from the keyboard JOptionPane.showInputDialog also reads String data

10 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Methods length() returns the number of characters in the string concat() can be used (like +) to concatenate strings equals() can be used to check if two strings contain the same sequence of characters compareTo() indicates whether one string comes before another

11 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comparing Strings for equality The == operator checks whether the same value is stored in two different variables. For reference variables, what is compared is two addresses To compare whether two strings have the same sequence of characters stored in them, use the equals method.

12 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comparing with == s1==s2 is true s1==s3 is false s1==s4 is true s2==s3 is false s2==s4 is true s2==s4 is false String s1, s2, s3, s4; s1 = "java"; s2 = "java"; s3 = new String("java"); s4 = s1;

13 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Different Reference Variables Can Reference Equal Strings Comparisons s1==s2 is false s1.equals(s2) is true String s1 = new String( "Help"); String s2 = new String( "Help");

14 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comparing Strings with compareTo compareTo does a character by character comparison –uses unicode values to determine order –case-sensitive s1.CompareTo(s2) is –negative if s1 comes before s2 –0 if s1 and s2 have the same sequence of characters –positive if s1 comes after s2 Examples "Hello" comes after "Goodbye" "hello" comes after "Hello" "SMITH" comes after "JONES" "123" comes after "1227" "Beehive" comes before "Behop" "123" comes before "abc"

15 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More String methods indexOf( char ch), indexOf( String str) return the position of a character or sequence of characters in the String –Returns -1 if absent –Overloaded versions of indexOf and lastIndexOf toUpperCase(), toLowerCase() return strings with all letters of the same case

16 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Substrings substring( int start, int next) returns a String containing the characters with indexes start through next-1 in the original String –"appaloosa".substring( 2, 5) ---> "pal" –"Helpless".substring( 0, 4) ---> "Help" –"Helpless".substring( 4) ---> "less"

17 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Character Class Like the other primitive types, there is a wrapper class, Character, for the char type –Can be used to create objects with a single char value for data –Has class methods useful for handling character data

18 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Character Methods Boolean methods that test whether a character belongs to a particular group of characters –isDigit, isLetter, isWhitespace, isUpperCase, isLowerCase Methods to convert characters from one case to another –toUpperCase, toLowerCase

19 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Converting Primitive Values to Strings String class has valueOf method which is overloaded to work for any primitive type – String valueOf( boolean b) – String valueOf( int I) – String valueOf( double d) – String valueOf( char c) Wrapper classes have a static toString method – String toString( boolean b) in the Boolean class for example

20 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Converting Strings to Primitive Values Wrapper class have methods to convert a String to a primitive value –Integer.parseInt( String intString) –Integer.valueOf( String intString) If the string is not appropriate for the primitive type, an error will occur

21 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 StringBuffer Class A StringBuffer is very similar to a String except that a StringBuffer object can be modified This means you can add, insert or replace characters within the StringBuffer rather than having to create a new object for each change. A StringBuffer is created with a capacity which is the number of characters it can hold. –The capacity can change if necessary

22 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating a StringBuffer StringBuffer() creates a StringBuffer with a capacity of 16 StringBuffer(int length) creates a StringBuffer with a capacity of length StringBuffer( String str) creates a StringBuffer with a capacity of str.length() + 16

23 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Initial Storage of a StringBuffer Object StringBuffer str = new StringBuffer( "This cannot be");

24 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Modifying the StringBuffer str.insert( 4, " I know"); str.replace( 12, 18, "to");

25 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The StringBuffer After the Append str.append( "correct");


Download ppt "©2004 Brooks/Cole Chapter 7 Strings and Characters."

Similar presentations


Ads by Google