1 / 67

COP3502 Programming Fundamentals for CIS Majors 1

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi. Exam Will be graded next week HW, PA Will be assigned on Monday. Chapter 9 String manipulation String StringBuilder StringBuffer Character Files Read Write.

kuniko
Download Presentation

COP3502 Programming Fundamentals for CIS Majors 1

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. COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

  2. Exam • Will be graded next week • HW, PA • Will be assigned on Monday

  3. Chapter 9 • String manipulation • String • StringBuilder • StringBuffer • Character • Files • Read • Write

  4. Suppose you need to write a program to replace all occurrences of a word with a new word in a file. • How do you solve this problem?

  5. String

  6. //String is a class  String message = new String("Welcome to Java"); • String is a reference type (class) //Since strings are used frequently, Java provides a //shorthand initializer for creating a string: String message = "Welcome to Java"; //You can also create a string from an array of // characters (char) char[] charArray = {‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’}; String message = newString(charArray);

  7. A String object is immutable • i.e. its contents cannot be changed. • Does the following code change the contents of the string? String s = "Java"; s = "HTML";

  8. String s = "Java"; s = "HTML";

  9. To improve efficiency and save memory: • JVM uses a unique instance for string literals with the same character sequence. • Such an instance is called interned.

  10. If you use the string initializer, no new object is created, if the interned object is already created.

  11. To compare strings, do not use ==

  12. String s1 = new String("Welcome"); String s2 = "Welcome"; if(s1.equals(s2)) { // s1 and s2 have the same contents } if(s1 == s2) { // s1 and s2 have the same reference } Difference between “==“ vs. “equals()”

  13. String s1 = new String("Welcome"); String s2 = "Welcome"; if (s1.compareTo(s2) > 0) { // s1 is greater than s2 // lexicographically, i.e. unicode order } else if (s1.compareTo(s2) == 0) { // s1 and s2 have the same contents } else // s1 is less than s2 To compare strings, do not use >=, <=

  14. Length • Retrieving individual characters • Concatenating string

  15. Finding string length using the length()method: String message = "Welcome"; intmsgLength= message.length(); //7

  16. Retrieving individual characters in a string: • Do not use message[0] • Use message.charAt(index) • Index starts from 0

  17. String s3 = s1.concat(s2); • String concatenation • String concatenation • Both have the same effect String s3 = s1 + s2; s1 + s2 + s3 + s4 + s5 = (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);

  18. String extraction

  19. You can extract a single character from a string using the charAt() method. • You can extract a substring from a string using the substring() method. String Message = "Welcome to Java"; String s2 = Message.substring(0, 11) + "HTML";

  20. Converting, replacing, and splitting String

  21. String s1 = "Welcome".toLowerCase(); //s1 is a new String: “welcome” String s2 = "Welcome".toUpperCase(); //s2 is a new string: “WELCOME” String s3 = " Welcome ".trim(); //s3 is a new string: “Welcome” String s4 = "Welcome".replace('e', 'A'); //s4 is a new string: “WAlcomA” String s5 = "Welcome".replaceFirst("e", "AB"); //returns a new string, WABlcome. String s6 = "Welcome".replaceAll("e", "AB"); //s6 is a new string: “WABlcomAB” String s7 = "Welcome".replaceAll("el", "AB"); //s7 is a new string: “WABlcome” • Converting, replacing, and splitting String

  22. String[] tokens = "Java#HTML#Perl".split("#“,0); for(inti = 0; i < tokens.length; i++) System.out.print(tokens[i] + " "); • The following code displays “Java HTML Perl”

  23. You can match, replace, or split a string by specifying a pattern. • This is an extremely useful and powerful feature, commonly known as regular expression. • Regular expression is complex to beginning students. For this reason, only two simple patterns are used in this section.

  24. "Java".matches("Java"); "Java".equals("Java"); • Regular expression: "Java is fun".matches("Java.*"); "Java is cool".matches("Java.*");

  25. The replaceAll(), replaceFirst(), and split()methods can be used with a regular expression.

  26. For example, the following statement returns a new string that replaces $, +, or #by the string NNN. • Here the regular expression [$+#] specifies a pattern that matches $, +, or #. • So, the output is “aNNNbNNNNNNc”. String s = "a+b$#c".replaceAll("[$+#]", "NNN"); System.out.println(s);

  27. The following code splits the string into an array of strings delimited by some punctuation marks.

  28. Finding a Character or a Substring in a String

  29. "Welcome to Java".indexOf('W') • returns 0. • "Welcome to Java".indexOf('x') • returns -1. • "Welcome to Java".indexOf('o', 5) • returns 9. • "Welcome to Java".indexOf("come") • returns 3. • "Welcome to Java".indexOf("Java", 5) • returns 11. • "Welcome to Java".indexOf("java", 5) • returns -1. • "Welcome to Java".lastIndexOf('a') • returns 14.

  30. HW7 posted • PA5 posted • Game of life

  31. String • Construction • Comparison • Find, substring

  32. A String is not an array! • But, can be converted.

  33. The String class provides several static valueOf() methods for converting a character, an array of characters, and numeric values to strings. • These methods have the same name valueOf with different argument types. • For example, to convert a double value to a string, use String.valueOf(5.44) • The return value is string “5.44”.

  34. The String class provides several static valueOf() methods

  35. Checking whether a string is a palindrome: • Astring that reads the same forward and backward. • Dad, Mom, noon Run CheckPalindrome

  36. Palindrome in ancient world

  37. Character

  38. Java provides a wrapper class for each primitive data type • All in java.lang package • Primitive data values can be treated as objects • char • Primitive data type • Character class • Wrapper

  39. Character class

  40. Character charObject = new Character('b'); • Character class • charObject.compareTo(new Character('a')) returns 1 • charObject.compareTo(new Character('b')) returns 0 • charObject.compareTo(new Character('c')) returns -1 • charObject.compareTo(new Character('d') returns –2 • charObject.equals(new Character('b')) returns true • charObject.equals(new Character('d')) returns false

  41. This example gives a program that counts the number of occurrence of each letter in a string. Assume the letters are not case-sensitive. Run CountEachLetter

  42. StringBuilder & StringBuffer

  43. The StringBuilder/StringBuffer class is an alternative to the String class. • In general, a StringBuilder/StringBuffer can be used wherever a string is used. • StringBuilder/StringBufferis more flexible than String. • You can add, insert, or append new contents into a string buffer • whereas the value of a String object is fixed once the string is created.

  44. The StringBuilder class

  45. The StringBuilder class

  46. StringBuffer is very similar toStringBuilder • It has synchronized methods • Use it in mutli-threaded applications

  47. Ignoring non-alphanumeric characters when checking palindromes Run PalindromeIgnoreNonAlphanumeric

  48. Command Line Arguments

  49. You can call a regular method by passing actual parameters. Can you pass arguments to main? • Of course, yes. For example, the main method in class B is invoked by a method in A, as shown below:

More Related