1 / 47

Introduction

Section 12.1. Introduction. String Processing. Word processing term papers, writing memoirs, sending email messages, responding to surveys, placing online orders and registering products all involve string processing .

gusty
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 12.1 Introduction

  2. String Processing Word processing term papers, writing memoirs, sending email 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.

  3. 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: ! @ # $ % ^ & * ( ) _ +

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

  5. Section 12.2 Constructing String Objects

  6. 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;

  7. // Java1201.java // 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(); } }

  8. Section 12.3 String Concatenation

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

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

  11. // 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(); } }

  12. Section 12.4 Working with Substrings

  13. // 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(); } } Do not confuse the length() method of String with the length field of a Java static Array!

  14. // 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(); } }

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

  16. // 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(); } }

  17. String Method charAt String s = "Texas"; int k = 2; char letter1 = s.charAt(k); char letter2 = s.charAt(2); 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'.

  18. // 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(); } }

  19. // 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(); } }

  20. 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); s3becomes "var"

  21. // 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(); } }

  22. By the way, it is the State Fish of Hawaii. String MethodsindexOf & 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!

  23. Section 12.5 Changing Strings

  24. // Java1209.java // 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(); } } NOTE: The replace method does NOT alter String s1 at ALL. It creates a new String which is returned and stored in s2.

  25. 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 s2becomes "badab".

  26. // 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);

  27. System.out.println("\nStrings Converted to All Uppercase"); System.out.println("----------------------------------"); 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("----------------------------------"); 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(); } }

  28. String Methods toUpperCaseand toLowerCase s1 = s2.toUpperCase(); s3 = s1.toLowerCase(); Method toUpperCase returns a String where all letters are upper-case. Method toLowerCase returns a Stringwhere 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.

  29. 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);

  30. Section 12.6 Converting Strings

  31. // Java1211.java // 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(123.321); 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(); } }

  32. String static Method valueOf String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); 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.

  33. // 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 = "123.456"; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); double sum = n1 + n2; System.out.println(n1 + " + " + n2 + " = " + sum); System.out.println(); } }

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

  35. Section 12.7 Comparing Strings

  36. // 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); else System.out.println(s1 + " does not equals " + s3); System.out.println(); } }

  37. // 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); else System.out.println(s1 + " does not equals " + s3); System.out.println(); } }

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

  39. What Is Going On? Part 2 The equality operator == does not work with objects because it compares the Shallow Values which are memory addresses.

  40. What Is Going On? Part 3 The equals method should be used with objects like Strings because it compares the Deep Values which is the actual information stored.

  41. TheBottom 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.

  42. // Java1215.java // 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(); } }

  43. // 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"); System.out.println(); } }

  44. // 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"); System.out.println(); } } Note that the value of s1 changed from “apple” to “APPLE”.

  45. // 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"); System.out.println(); } } Note that the value of s1 did not change this time.

  46. String methodsequals and compareTo if (s1.equals(s2)) intdifference = s3.compareTo(s4); Method equals returns true if s1 is equal to s2, and falseotherwise. Method compareTo returns an intvalue 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 befores4. If the int value is positive, s3 goes afters4.

More Related