Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings 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.

Similar presentations


Presentation on theme: "Strings 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."— Presentation transcript:

1 Strings 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/ http://java.sun.com/j2se/1.5.0/docs/api/ A String is a sequence of letters

2 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();

3 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 23 Primitive variable Example: int x = 23; “abc” Address of “abc Object Variable Example: String str = “abc”; null Object Variable with nothing in it. Example: String str; x str

4 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

5 String Method Examples 1.String s = “ abcdefg “.trim() puts “abcdefg” into s 2.int i = “apple jack”.length() puts a 10 into i 3.String s = “abcdef”.substring(2,4); puts “cd” into s 4.String s = “abracadabra”.replace(‘a’,’b’); puts “bbrbcbdbrb” into s 5.char c = “abcdefg”.charAt(4); puts a ‘e’ into c 6.int i = “abracadabra”.indexOf(‘a’,4); puts a 5 into i. 7.int i = “abracadabra”.indexOf(‘e’,0); puts a -1 into i 8.String s = “abcdef”.toUpperCase(); puts “ABCDEF” into s 9.Int i = “aeiouAEIOU’.indexOf(c); returns >=0 if c is a vowel.

6 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”

7 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?


Download ppt "Strings 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."

Similar presentations


Ads by Google