Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.

Similar presentations


Presentation on theme: "Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of."— Presentation transcript:

1 Chapter 3A Strings

2 Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of package containing class (java.lang) 3.Name of method (pow), its parameters (int a, int b), and function (a^b) 4.Remember to import the appropriate package.

3 Program Example import java.lang.*; public class TestPowerFunction { // main(): application entry point static public void main(String args[]) { double answer; answer = Math.pow(2,3); System.out.println("ANSWER 2^3) = " + answer); } }

4 The class String String variables are reference variables Given String name; –Equivalent Statements: name = new String("Lisa Johnson"); name = “Lisa Johnson”;

5 The class String The String object is an instance of class String The value “Lisa Johnson” is instantiated The address of the value is stored in name String methods are called using the dot operator

6 Commonly Used String Methods LENGTH: –int length(String str) Length of string. GETTING PARTS: –char charAt(int index) Character at position index. –String substring(int startIndex, int stopIndex) String (substring) starting at startIndex until stopIndex-1. –String substring(int startIndex) String (substring) starting at startIndex until end of the string.

7 Commonly Used String Methods SEARCHING: All ‘ indexOf ’ methods return int values. All ‘ indexOf ’ methods return -1 if the string/char is not found. –int indexOf(char ch) Index of 1 st occurrence of the character. –int indexOf(char ch, int pos) Index of 1 st occurrence of the character. Parameter pos specifies from where to begin the search. –int indexOf(String str) Index of 1 st occurrence of the string. –int indexOf(String str, int pos) Index of 1 st occurrence of the string. Parameter pos specifies from where to begin the search.

8 Commonly Used String Methods CREATING A NEW STRING FROM THE ORIGINAL: Returns a String. –String toLowerCase() New String with all chars lowercase –String toUpperCase() New String with all chars uppercase –String replace(char c1, char c2) New String in which every occurrence of c1 is replaced with c2.

9 Commonly Used String Methods COMPARISON: Use these instead of == and != –int compareTo(String str) Compares 2 strings character by character. Returns 0 / negative / positive –boolean equals(String str) Returns true if 2 strings have equal values. Otherwise false. –boolean equalsIgnoreCase(String str) Same as above, but upper & lower case are the same.

10 CLASS EXERCISE public class VariousStringMethods { public static void main (String[] args) { String sentence,str1,str2,str3; int index; sentence = "Now is the time for the birthday party"; //Line 1 System.out.println("Line 2: sentence = \"" + sentence + "\""); //Line 2 System.out.println("Line 3:The length of sentence = " + sentence.length()); //Line 3 System.out.println("Line 4:The character at index 16 in " + "sentence = " + sentence.charAt(16)); //Line 4

11 System.out.println("Line 5: The index of first t in " + "sentence = " + sentence.indexOf('t')); //Line 5 System.out.println("Line 6: The index of for in sentence " + "= " + sentence.indexOf("for")); //Line 6 System.out.println("Line 7: sentence in uppercase = \"" + sentence.toUpperCase() + "\""); //Line 7 index = sentence.indexOf("birthday"); //Line 8 str1 = sentence.substring(index, index + 14); //Line 9 System.out.println("Line 10: str1 = \"" + str1 + "\""); //Line 10 str2 = "Super " + sentence.substring(index, index + 14); //Line 11 System.out.println("Line 12: str2 = \"" + str2 + "\""); //Line 12 str3 = sentence.replace('t', 'T'); //Line 13 System.out.println("Line 14: str3 = \"" + str3 + "\""); //Line 14 }


Download ppt "Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of."

Similar presentations


Ads by Google