Week 5 Power Point Slides

Download Report

Transcript Week 5 Power Point Slides

Strings
A String is a sequence of letters
• In Java, strings are contained in an object that
is an instance of the String class.
• String in java is the name of a class. That’s why
it starts with a capital. It is NOT a reserved
word.
• The String class has lots of useful methods.
• You can look these up at:
http://java.sun.com/j2se/1.5.0/docs/api/
To declare a string
• The way we’ve seen before: String str = "abcdef";
• A new way: String str = new String("abcdef");
• Notes:
– In Java the reserved word new instantiates (creates) and
instance of a new object following the blueprint that its
class defines
– In the above example str contains an object of type string.
– To put a string into str, we can say things like:
str = "abc"; or str = IO.readString();
Comparing Objects
• Primitive variable types: byte,
short, int, long, float, double,
char, boolean
• Object variable types: thoes
created from a class like String
• Object variables and string
variables store differently in
memory
• Objects to be compared must
contain an equals method
• str1 == str2 compares memory
addresses, not contents
• str1.equals(str2) compares
contents
x
23
Primitive variable
Example: int x = 23;
str
Address of
"abc
"abc"
Object Variable
Example: String str = "abc";
str
null
Object Variable with nothing in it.
Example: String str;
String Methods
• str.trim() eliminates leading and trailing spaces from String, str.
• str.length() returns the number of characters in the String, str
• str.substring(x,y) returns the String starting at the x position up
to but not including the y position of String, str.
• str.replace(oldChar, newChar) replaces all occurrences of
oldChar by newChar in String, str
• str.charAt(x) returns the character at position x of String, str
• Str.indexOf(‘x’,y) returns the first index to where ‘x’ appears in
the String, str, starting from position y
• str.toUpperCase() and str.toLowerCase() changes the case of a
String str
String Method Examples
1.
2.
3.
4.
5.
6.
7.
8.
9.
String s = " abcdefg
".trim() puts "abcdefg" into s
int i = "apple jack".length() puts a 10 into i
String s = "abcdef".substring(2,4); puts "cd" into s
String s = "abracadabra".replace(‘a’,’b’); puts "bbrbcbdbrb"
into s
char c = "abcdefg".charAt(4); puts a ‘e’ into c
int i = "abracadabra".indexOf(‘a’,4); puts a 5 into i.
int i = "abracadabra".indexOf(‘e’,0); puts a -1 into i
String s = "abcdef".toUpperCase(); puts "ABCDEF" into s
Int i = "aeiouAEIOU’.indexOf(c); returns >=0 if c is a vowel.
Common Examples
• We want to do a loop until the user enters either a "q"
or "Q"
Solution: while (value.indexOf("qQ")<0) { … }
• We want to capitalize the first letter of a string and
lower case the rest
Solution: value = value.substring(0,1).toUpperCase()
+ value.substring(1).toLowerCase();
• We want to remove the space characters from a string
Solution: value = value.replaceAll("\\s+", "");
Lab Hints
• Make the first letter of a string capital
– Use the charAt() method to get the first letter
– Strings are immutable; therefore
str.charAt(0) = "A"; will not work
– str.substring(0,1).toUpperCase() will work as long as the string
has at least one character.
• To find the second name of: "first middle last"
– Use indexOf to find the spaces before
– Use substring and trim to get "last middle" and eliminate
leading and trailing spaces
– If no more spaces, then the form is "first last" not "first
middle last"
Review Questions
1. What is the difference between and object and a primitive
variable?
2. How do you find out information about the many classes provided
by Java?
3. String objects are immutable. What does this mean?
4. What is String concatenation?
5. Describe the use of the following methods:
–
–
–
–
–
Replace()
charAt()
indexOf()
toUpperCase() and toLowerCase()
trim()
6. How do you find the length of a string?
7. What is the index of the first character of a string?