Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Processing Word processing term papers, writing memoirs, sending email messages, responding to surveys, placing online orders and registering products.

Similar presentations


Presentation on theme: "String Processing Word processing term papers, writing memoirs, sending email messages, responding to surveys, placing online orders and registering products."— Presentation transcript:

1

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

5

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

9 Mathematical Addition 100 + 200 = 300 int x = 100; x += 200; x now stores 300.

10 String Concatenation “100” + “200” = “100200” String x = “100”; x += “200”; x now stores “100200”.

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

13 // Java1203.java // This program demonstrates the use of the 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(); } 0123456789 1011121314 Argentine Tango

14 // Java1204.java // This program demonstrates that in Java a // 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(); } 01234 Texas

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 using the 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(); } 01234 Texas

17 String Method charAt String s = "Texas"; int k = 2; char letter1 = s.charAt(k); char letter2 = s.charAt(2); 01234 Texas 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 method is used inside a 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(); } 0123456789 10111213 s1 Madam I'm Adam s2 madAm'ImadaM

19 // Java1207.java // This program demonstrates how to access specified characters of a string // with the 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(); } 0123456 Racecar

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); s3 becomes "var" s1 01234567 Aardvark

21 // Java1208.java // This program shows the method, which returns the index of the first // occurrence of the string argument, and the 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(); } s2 0123456789 101112131415161718192021 racecarin the carport s1 0123456 racecar

22 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 i ndexOf is lowercase, but in last I ndexOf it’s capital! s1 0123456789101112131415161718192021 humuhumunukunukuapua’a By the way, it is the State Fish of Hawaii.

23

24 // Java1209.java // This program demonstrates the 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 s2 becomes "badab".

26 // Java1210.java // This program demonstrates the String and // 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

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

30 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);

31

32 // Java1211.java // This program demonstrates the method of the String class, // which is shown to convert four data types to a string. // Note that is a static method and must be called using. 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(); }

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

34 // Java1212.java // This program converts string values to integer and double values // using the and methods of the // and 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(); }

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

36

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

38 // Java1214.java // This program demonstrates the 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(); }

39 int x = 10; int y = 10; int z = 20; What Is Going On? Part 1 x 10 y z 20

40 What Is Going On? Part 2 s1 @dff6ccd s2 @3b0eb0 dff6ccd Foxtrot 3b0eb0 Waltz s3 @18d107f 18d107f Foxtrot

41 What Is Going On? Part 3 s1 @dff6ccd s2 @3b0eb0 dff6ccd Foxtrot 3b0eb0 Waltz s3 @18d107f 18d107f Foxtrot

42 The Bottom Line If you are comparing simple data types like 2 int s, 2 double s, 2 char s or 2 boolean s, use the == operator. If you are comparing objects – and String s are objects – you need to use the equals method.

43 // Java1215.java // This program demonstrates the 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(); }

44 // Java1216.java // This program shows the 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(); }

45 // Java1217.java // This program cures the "case" issue of the previous program. // is used to convert all strings to CAPITAL letters so case-sensitivity is // no longer an issue. The 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(); }

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

47 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 "String Processing Word processing term papers, writing memoirs, sending email messages, responding to surveys, placing online orders and registering products."

Similar presentations


Ads by Google