©2004 Brooks/Cole Chapter 7 Strings and Characters.
-
Upload
magnus-butler -
Category
Documents
-
view
226 -
download
0
Transcript of ©2004 Brooks/Cole Chapter 7 Strings and Characters.
©2004 Brooks/Cole
Chapter 7
Strings and Characters
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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 instantiationmessage = new String("hello");• The String class has several constructors
(overloaded)
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
String Creation
String string1 = new String("Hello");
String string2 = new String("Hello there");
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
String Concatenation
• We can join two strings into a larger string using the + operatorString s1 = "Hot ";
String s2 = "Dog";
String s3 = s1 + s2;
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Concatenation creates a new String
String message = "Start";
message = message + "le";
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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.
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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;
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Different Reference Variables Can Reference Equal Strings
• Comparisonss1==s2 is false
s1.equals(s2) is true
String s1 = new String( "Help");
String s2 = new String( "Help");
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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"
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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"
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 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
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Initial Storage of a StringBuffer Object
StringBuffer str = new StringBuffer( "This cannot be");
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
Modifying the StringBuffer
str.insert( 4, " I know");
str.replace( 12, 18, "to");
Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005
The StringBuffer After the Append
str.append( "correct");