Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods

Similar presentations


Presentation on theme: "Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods"— Presentation transcript:

1 Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
PowerPoint Presentation created by: Mr. John L. M. Schram and Mr. Leon Schram Authors of Exposure Java

2 Section 12.1 Introduction

3 String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products all involve string processing. Every software package on the market includes string-processing components. Every programming language has special features that facilitate the manipulation of strings, and Java is no different.

4 String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters, numerical characters and a large set of characters for a variety of purposes like: ! @ # $ % ^ & * ( ) _ +

5 String Variables vs. String Literals
A string literal is a set of characters delimited with double quotations. String name = “John Smith”; name is the string variable. “John Smith” is the string literal.

6 Section 12.2 Constructing String Objects

7 Is String a Simple Data Type?
When you see statements like: which looks very similar to you might get the idea that String is a simple (or primitive) data type like int, double, char, and boolean. However, String is NOT a simple data type. String is a class. String name = “John Smith”; int x = 5;

8 Java1201.java s1: Tango s2: Tango s3: Tango s4: Tango s5: Tango
// This program demonstrates multiple ways to construct String objects. public class Java1201 { public static void main (String args[]) System.out.println("\nJava1201.java\n"); String s1 = "Tango"; System.out.println("s1: " + s1); String s2 = new String(); // Default constructor s2 = "Tango"; System.out.println("s2: " + s2); String s3 = new String("Tango"); // Overloaded constructor with String parameter System.out.println("s3: " + s3); String s4 = new String(s3); // Same constructor as s3 System.out.println("s4: " + s4); char dance[ ] = {'T','a','n','g','o'}; String s5 = new String(dance); // Overloaded constructor with char array parameter System.out.println("s5: " + s5); System.out.println(); } Java1201.java s1: Tango s2: Tango s3: Tango s4: Tango s5: Tango

9 Section 12.3 String Concatenation

10 Mathematical Addition
= 300 int x = 100; x += 200; x now stores 300. When used with int or double the plus sign ( + ) performs addition.

11 String Concatenation When used with Strings the plus
“100” + “200” = “100200” String x = “100”; x += “200”; x now stores “100200”. When used with Strings the plus sign ( + ) performs String Concatenation.

12 Java1202.java s3: Argentine Tango s6: 10002000 // Java1202.java
// This program shows how to concatenate strings using the + operator. public class Java1202 { public static void main (String args[]) System.out.println("\nJava1202.java\n"); String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println("s3: " + s3); String s4 = "1000"; String s5 = "2000"; String s6 = s4 + s5; System.out.println("s6: " + s6); System.out.println(); } Java1202.java s3: Argentine Tango s6:

13 Section 12.4 Working with Substrings

14 Do not confuse the length() method of String
// Java1203.java // This program demonstrates the use of the <length> method. public class Java1203 { public static void main (String args[]) System.out.println("\nJava1203.java\n"); String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println(s1 + " has " + s1.length() + " characters."); System.out.println(s2 + " has " + s2.length() + " characters."); System.out.println(s3 + " has " + s3.length() + " characters."); System.out.println(); } Java1203.java Argentine has 9 characters. Tango has 5 characters. Argentine Tango has 15 characters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 A r g e n t i T a o Do not confuse the length() method of String with the length field of a Java static Array!

15 // This program demonstrates that in Java a <String>
// Java1204.java // This program demonstrates that in Java a <String> // is NOT an Array of Characters. public class Java1204 { public static void main (String args[]) System.out.println("\nJava1204.java\n"); String state = "Texas"; System.out.println( state[2] ); System.out.println(); } 1 2 3 4 T e x a s

16 Are Strings Really Arrays of Characters?
In many computer languages, like C++, you can access the individual characters in a string by using the index[ ] operator. That is because in those languages, a string is implemented as an array of characters. In Java this is NOT the case. String is a class and like most classes, the String class has methods. One of them is charAt which is what you use to access a character At a specific index.

17 // This program shows the proper way to access an individual
// Java1205.java // This program shows the proper way to access an individual // character in a <String> using the <charAt> method. public class Java1205 { public static void main (String args[]) System.out.println("\nJava1205.java\n"); String state = "Texas"; System.out.println( state.charAt(2) ); System.out.println(); } 1 2 3 4 T e x a s Java1205.java x

18 String Method charAt String s = "Texas"; int k = 2;
char letter1 = s.charAt(k); char letter2 = s.charAt(2); 1 2 3 4 T e x a s Method charAt returns the character stored at the integer index location of the string object s. The index parameter can be an integer variable or an integer constant. The first character is at index 0. In this case, both letter1 and letter2 become 'x'.

19 for (int k = n; k >= 0; k--) s2 += s1.charAt(k); s1 M a d m I ' A
// Java1206.java // This program demonstrates how to create one string object that is the // reverse of another. The <charAt> method is used inside a <for> loop. public class Java1206 { public static void main (String args[]) System.out.println("\nJava1206.java\n"); String s1 = "Madam I'm Adam"; String s2 = ""; int n = s1.length() - 1; for (int k = n; k >= 0; k--) s2 += s1.charAt(k); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 s1 M a d m I ' A s2 Java1206.java s1: Madam I'm Adam s2: madA m'I madaM

20 R a c e r Java1207.java Race ace ce ceca eca car // Java1207.java
// This program demonstrates how to access specified characters of a string // with the <substring(SI,EI)> method, where SI is the Starting Index and // EI is one more than the Ending Index. public class Java1207 { public static void main (String args[]) System.out.println("\nJava1207.java\n"); String s = "Racecar"; System.out.println(s.substring(0,4)); System.out.println(s.substring(1,4)); System.out.println(s.substring(2,4)); System.out.println(s.substring(2,6)); System.out.println(s.substring(3,6)); System.out.println(s.substring(4,7)); System.out.println(); } 1 2 3 4 5 6 R a c e r Java1207.java Race ace ce ceca eca car

21 String Method substring
s1 = “Aardvark”; s2 = s1.substring(j,k); Method substring returns a set of consecutive characters from String s1, starting at index j, and ending at index k-1. s3 = s1.substring(4,7); s3 becomes "var" s1 1 2 3 4 5 6 7 A a r d v k

22 s1 r a c e s2 r a c e i n t h p o int index1 = s1.indexOf(s3);
Java1208.java With "racecar" car starts at 4 and last shows up at 4 With "racecar in the carport" car starts at 4 and last shows up at 15 With "car" Qwerty shows up at -1 // Java1208.java // This program shows the <indexOf> method, which returns the index of the first // occurrence of the string argument, and the <lastIndexOf> method, which returns the // index of the last occurrence of the string argument. public class Java1208 { public static void main (String args[]) System.out.println("\nJava1206.java\n"); String s1 = "racecar"; String s2 = "racecar in the carport"; String s3 = "car"; int index1 = s1.indexOf(s3); int index2 = s1.lastIndexOf(s3); int index3 = s2.indexOf(s3); int index4 = s2.lastIndexOf(s3); int index5 = s1.indexOf("qwerty"); System.out.println("With \"" + s1 + "\" car starts at " + index1 + " and last shows up at " + index2); System.out.println("With \"" + s2 + "\" car starts at " + index3 + " and last shows up at " + index4); System.out.println("With \"" + s3 + "\" Qwerty shows up at " + index5); System.out.println(); } s1 1 2 3 4 5 6 r a c e s2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 r a c e i n t h p o

23 String Methods indexOf & lastIndexOf
indexOf returns the first occurrence of a substring s1.indexOf(“hum”); returns 0 s1.indexOf(“ku”); returns 10 s1.indexOf(“qwerty”); returns -1 lastIndexOf returns the last occurrence of a substring s1.lastIndexOf(“hum”); returns 4 s1.lastIndexOf(“ku”); returns 14 s1.lastIndexOf(“qwerty”); returns -1 Note: The i in indexOf is lowercase, but in lastIndexOf it’s capital! s1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 h u m n k a p By the way, it is the State Fish of Hawaii.

24 Section 12.5 Changing Strings

25 Java1209.java s1: racecar s2: lacecal
// This program demonstrates the <replace(Old,New)> String // method, which replaces all occurrences of the Old character with // the New character. This method creates a new String object with // the replaced characters. public class Java1209 { public static void main (String args[]) System.out.println("\nJava1209.java\n"); String s1 = "racecar"; String s2 = s1.replace('r','l'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); } Java1209.java s1: racecar s2: lacecal NOTE: The replace method does NOT alter s1 AT ALL. It creates a copy of the string which is returned and stored in s2.

26 String Method replace String s2 = s1.replace('m','b');
Method replace returns a string such that every occurrence of the old character 'm' is replaced by the new character 'b' . If s1 == "madam" then s2 becomes "badab".

27 // Java1210.java // This program demonstrates the String <toUpperCase> and // <toLowerCase> methods. public class Java1210 { public static void main (String args[]) System.out.println("\nJava1210.java\n"); String s1 = "racecar"; String s2 = "RaCeCaR"; String s3 = "RACECAR100"; String s4 = s1.toUpperCase(); String s5 = s2.toUpperCase(); String s6 = s3.toUpperCase(); String s7 = s1.toLowerCase(); String s8 = s2.toLowerCase(); String s9 = s3.toLowerCase(); System.out.println("\nOriginal Strings"); System.out.println(" "); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3);

28 System.out.println("\nStrings Converted to All Uppercase");
System.out.println("s4: " + s4); System.out.println("s5: " + s5); System.out.println("s6: " + s6); System.out.println("\nStrings Converted to All Lowercase"); System.out.println("s7: " + s7); System.out.println("s8: " + s8); System.out.println("s9: " + s9); // This is what you need to do to alter the original strings. s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); s3 = s3.toLowerCase(); System.out.println("\nOriginal Strings Again"); System.out.println(" "); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println(); }

29 Java1210.java Original Strings s1: racecar s2: RaCeCaR s3: RACECAR100 Strings Converted to All Uppercase s4: RACECAR s5: RACECAR s6: RACECAR100 Strings Converted to All Lowercase s7: racecar s8: racecar s9: racecar100 Original Strings Again s1: RACECAR s2: RACECAR s3: racecar100

30 String Methods toUpperCase and toLowerCase
s1 = s2.toUpperCase(); s3 = s1.toLowerCase(); Method toUpperCase returns a String where all letters are upper-case. Method toLowerCase returns a String where all letters are lower-case. Any characters that are not letters will be ignored by both methods and returned in their same relative String position.

31 Altering the Original String
Remember, String methods do not alter the original String object. They return an altered copy of the String object. To alter the original String object, you need a statement that assigns the new copy back to the original object. Examples: s1 = s1.toUpperCase(); s2 = s2.toLowerCase(); s3 = s3.replace('m','b'); s4 = s4.substring(1,5);

32 Section 12.6 Converting Strings

33 Java1211.java s1: 1000 s2: 123.321 s3: true s4: A s5: 1000123.321
// This program demonstrates the <valueOf> method of the String class, // which is shown to convert four data types to a string. // Note that <valueOf> is a static method and must be called using <String.valueOf>. public class Java1211 { public static void main (String args[]) System.out.println("\nJava1211.java\n"); String s1 = String.valueOf(1000); String s2 = String.valueOf( ); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); String s5 = s1 + s2; System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println("s4: " + s4); System.out.println("s5: " + s5); System.out.println(); } Java1211.java s1: 1000 s2: s3: true s4: A s5:

34 String static Method valueOf
String s1 = String.valueOf(1000); String s2 = String.valueOf( ); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); Method valueOf converts the provided parameter and returns a string. Four overloaded valueOf methods are displayed. Note that the valueOf method is a static method (or class method) that is called with the String class identifier.

35 // Java1212.java // This program converts string values to integer and double values // using the <parseInt> and <parseDouble> methods of the <Integer> // and <Double> classes. public class Java1212 { public static void main (String args[]) System.out.println("\nJava1212.java\n"); String s1 = "123456"; String s2 = " "; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); double sum = n1 + n2; System.out.println(n1 + " + " + n2 + " = " + sum); System.out.println(); } Java1212.java =

36 Integer static method parseInt and Double static method parseDouble
int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); Method parseInt converts a String into an int. Method parseDouble converts a String into a double. Parameters that include non-numerical characters will compile, but will cause a run-time error.

37 Section 12.7 Comparing Strings

38 Enter a string ===>> Foxtrot Foxtrot does not equal Waltz
// Java1213.java // This program checks equality of strings using the == operator. // The == operator does not work correctly with string objects. public class Java1213 { public static void main (String args[]) System.out.println("\nJava1213.java\n"); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = Expo.enterString(); if (s1 == s2) System.out.println(s1 + " equals " + s2); else System.out.println(s1 + " does not equals " + s2); if (s1 == s3) System.out.println(s1 + " equals " + s3); System.out.println(s1 + " does not equals " + s3); System.out.println(); } Java1213.java Enter a string ===>> Foxtrot Foxtrot does not equal Waltz Foxtrot does not equal Foxtrot

39 Enter a string ===>> Foxtrot Foxtrot does not equal Waltz
// Java1214.java // This program demonstrates the <equals> method, which is capable of // testing equality of string objects correctly. public class Java1214 { public static void main (String args[]) System.out.println("\nJava1214.java\n"); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = Expo.enterString(); if (s1.equals(s2)) System.out.println(s1 + " equals " + s2); else System.out.println(s1 + " does not equals " + s2); if (s1.equals(s3)) System.out.println(s1 + " equals " + s3); System.out.println(s1 + " does not equals " + s3); System.out.println(); } Java1214.java Enter a string ===>> Foxtrot Foxtrot does not equal Waltz Foxtrot equals Foxtrot

40 What Is Going On? Part 1 The equality operator == works with
int x = 10; int y = 10; int z = 20; x 10 y 10 z 20 The equality operator == works with primitive data types like int. x == y x != z

41 What Is Going On? Part 2 s1 @dff6ccd s2 @3b0eb0 s3 @18d107f dff6ccd Foxtrot 3b0eb0 Waltz 18d107f Foxtrot The equality operator == does not work with objects because it compares the Shallow Values which are memory addresses.

42 What Is Going On? Part 3 s1 @dff6ccd s2 @3b0eb0 s3 @18d107f dff6ccd Foxtrot 3b0eb0 Waltz 18d107f Foxtrot The equals method should be used with objects like Strings because it compares the Deep Values which is the actual information stored.

43 The Bottom Line If you are comparing simple data types like 2 ints, 2 doubles, 2 chars or 2 booleans, use the == operator. If you are comparing objects – and Strings are objects – you need to use the equals method.

44 Java1215.java value1: -25 value2: 0 value3: 25 value4: -1
// This program demonstrates the <compareTo> method, which returns an integer value. // The returned value indicates which string alphabetically goes before the other. // If the value is negative, the original string goes first. // If the value is positive, the parameter string goes first. // If the value is zero, both strings are equal. public class Java1215 { public static void main (String args[]) System.out.println("\nJava1215.java\n"); String s1 = "AARDVARK"; String s2 = "ZEBRA"; String s3 = "AARDVARK"; String s4 = "BART"; int value1 = s1.compareTo(s2); int value2 = s1.compareTo(s3); int value3 = s2.compareTo(s1); int value4 = s1.compareTo(s4); System.out.println("value1: " + value1); System.out.println("value2: " + value2); System.out.println("value3: " + value3); System.out.println("value4: " + value4); System.out.println(); } Java1215.java value1: -25 value2: 0 value3: 25 value4: -1

45 public static void main (String args[])
// Java1216.java // This program shows the <compareTo> method being used to alphabetize 2 strings. // Note: This will not work properly if the strings are not all in the same "case". public class Java1216 { public static void main (String args[]) System.out.println("\nJava1216.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); System.out.print("Enter second string ===>> "); String s2 = Expo.enterString(); System.out.println(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); } Java1216.java Enter first string ===>> apple Enter second string ===>> banana apple goes before banana Java1216.java Enter first string ===>> zebra Enter second string ===>> neon neon goes before zebra Java1216.java Enter first string ===>> qwerty Enter second string ===>> qwerty Both strings are equal Java1216.java Enter first string ===>> apple Enter second string ===>> ZEBRA ZEBRA goes before apple

46 Note that the value of s1 changed from “apple” to “APPLE”.
// Java1217.java // This program cures the "case" issue of the previous program. // <toUpperCase> is used to convert all strings to CAPITAL letters so case-sensitivity is // no longer an issue. The <toLowerCase> methods could have been used just as well. public class Java1217 { public static void main (String args[]) System.out.println("\nJava1217.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); System.out.print("Enter second string ===>> "); String s2 = Expo.enterString(); System.out.println(); s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); } Java1217.java Enter first string ===>> apple Enter second string ===>> ZEBRA APPLE goes before ZEBRA Note that the value of s1 changed from “apple” to “APPLE”.

47 Note that the value of s1 did not change this time.
// Java1218.java // This program shows an alternate cure for the "case" issue of program Java1216.java. // With this solution, the 2 strings do not get altered as with the previous program. public class Java1218 { public static void main (String args[]) System.out.println("\nJava1218.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); System.out.print("Enter second string ===>> "); String s2 = Expo.enterString(); System.out.println(); int difference = s1.toUpperCase().compareTo(s2.toUpperCase()); if (difference < 0) System.out.println(s1 + " goes before " + s2); else if (difference > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); } Java1218.java Enter first string ===>> apple Enter second string ===>> ZEBRA apple goes before ZEBRA Note that the value of s1 did not change this time.

48 String methods equals and compareTo
if (s1.equals(s2)) int difference = s3.compareTo(s4); Method equals returns true if s1 is equal to s2, and false otherwise. Method compareTo returns an int value based on the difference between s3 and s4. If the int value is 0, s3 and s4 are equal. If the int value is negative, s3 goes before s4. If the int value is positive, s3 goes after s4.


Download ppt "Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods"

Similar presentations


Ads by Google