Published on

1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 07slide

    1. 1. Chapter 7 Strings <ul><li>Processing strings using the String class, the StringBuffer/StringBuilder class, and the StringTokenizer class. </li></ul><ul><li>Use the String class to process fixed strings. </li></ul><ul><li>Use the StringBuffer/StringBuilder class to process flexible strings. </li></ul><ul><li>Use the StringTokenizer class to extract tokens from a string. </li></ul><ul><li>Use the command-line arguments. </li></ul>
    2. 2. String <ul><li>String </li></ul><ul><ul><li>a sequence of characters </li></ul></ul><ul><ul><li>Treated as object in Java </li></ul></ul>
    3. 3. The String Class <ul><li>Declaring a String: </li></ul><ul><ul><li>String message = &quot;Welcome to Java!&quot; </li></ul></ul><ul><ul><li>String message = new String(&quot;Welcome to Java!“); </li></ul></ul><ul><ul><li>String s = new String(); </li></ul></ul><ul><li>String Comparisons (equals, compareTo) </li></ul><ul><li>Substrings (substring(index), substring(start, end)) </li></ul><ul><li>String Concatenation (concat, +) </li></ul><ul><li>String Length (length()) </li></ul><ul><li>Retrieving Individual Characters in a string </li></ul>
    4. 4. String Comparisons <ul><li>equals </li></ul><ul><li>String s1 = &quot;Welcome&quot;; </li></ul><ul><li>String s2 = &quot;welcome&quot;; </li></ul><ul><li>if (s1.equals(s2)) </li></ul><ul><li>{ // s1 and s2 have the same contents } </li></ul><ul><li>if (s1 == s2) </li></ul><ul><li>{ </li></ul><ul><li>// s1 and s2 have the same reference </li></ul><ul><li>} </li></ul>
    5. 5. Strings are immutable <ul><li>Strings are immutable. The contents of a string cannot be changed. To improve efficiency and save memory, Java Virtual Machine makes a great effort to identify the identical strings and store them in the same memory location, but it does not guarantee that all of the same strings are stored in the same memory location. Therefore, you must use the equals method to test whether two strings have the same contents, and the == operator to test whether the two strings have the same references (that is, point to the same memory location). </li></ul>
    6. 6. Strings are immutable <ul><li>String object is immutable; its contents cannot be changed. So what happen in the following code? </li></ul><ul><li>String s = “Java”; </li></ul><ul><li>s = “HTML”; </li></ul>
    7. 7. String Comparisons, cont. <ul><li>compareTo(Object object) </li></ul><ul><li>String s1 = “abc&quot;; </li></ul><ul><li>String s2 = “abg&quot;; </li></ul><ul><li>if (s1.compare(s2) > 0) </li></ul><ul><li>{ // s1 is greater than s2 lexicographically } </li></ul><ul><li>else if (s1.compare(s2) == 0) </li></ul><ul><li>{ // s1 is equal to s2 lexicographically } </li></ul><ul><li>else </li></ul><ul><li>// s1 is less than s2 lexicographically </li></ul><ul><li>//answer = -4 </li></ul>
    8. 8. Substrings <ul><li>public String substring(int beginIndex, int endIndex) </li></ul><ul><ul><li>Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. </li></ul></ul><ul><li>public String substring (int beginIndex) </li></ul><ul><ul><li>Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. </li></ul></ul>
    9. 9. Substrings <ul><li>String s1 = &quot;Welcome to Java&quot;; </li></ul><ul><li>String s2 = s1.substring(0,11) + &quot;HTML&quot;; </li></ul><ul><li>// Welcome to HTML </li></ul><ul><li>String s3 = s1.substring(11) </li></ul><ul><li>// Java </li></ul>
    10. 10. String Concatenation <ul><li>String s3 = s1.concate(s2); </li></ul><ul><li>Is equivalent to: </li></ul><ul><li>String s3 = s1 + s2; </li></ul>
    11. 11. Finding String Length <ul><li>Finding string length using the length() method: </li></ul><ul><li>message = &quot;Welcome&quot;; </li></ul><ul><li>message.length() (returns 7 ) </li></ul>
    12. 12. Retrieving Individual Characters in a String <ul><li>Use message.charAt(index) </li></ul><ul><li>Index between 0 to message.length() – 1 </li></ul><ul><li>message.charAt(0); //W </li></ul>
    13. 13. String Conversions <ul><li>The contents of a string cannot be changed once the string is created. But you can convert a string to a new string using the following methods: </li></ul><ul><li>toLowerCase </li></ul><ul><li>toUpperCase </li></ul><ul><li>trim </li></ul><ul><li>replace(oldChar, newChar) </li></ul>
    14. 14. Convert char and numbers to Strings <ul><li>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 char, char[], double, long, int, and float. For example, to convert a double value to a string, use String.valueOf(5.44). The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’. </li></ul><ul><li>To convert string to primitive data types: </li></ul><ul><li>Double.parseDouble(str); </li></ul><ul><li>Integer.parseInt(str); </li></ul>
    15. 15. For other methods in String class refer to: <ul><li>http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html </li></ul><ul><li>s.indexOf(); </li></ul><ul><li>s1.toCharArray(); </li></ul>
    16. 16. Example 7.1 Finding Palindromes <ul><li>Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward. </li></ul>CheckPalindrome Run
    17. 17. The StringBuffer Class <ul><li>The StringBuffer class is an alternative to the String class. In general, a string buffer can be used wherever a string is used. StringBuffer is more flexible than String . You can add, insert, or append new contents into a string buffer. However, the value of a string is fixed once the string is created. </li></ul>
    18. 18. The Character Class <ul><li>Java provides a wrapper class for every primitive data type: Character, Boolean, Byte, Short, Integer, Long, Float, Double. </li></ul><ul><li>java.lang package </li></ul><ul><li>Character class </li></ul><ul><ul><li>Character c = new Character (‘a’); </li></ul></ul>
    19. 19. StringBuilder/StringBuffer classes <ul><li>Alternative to String class </li></ul><ul><li>Can be used wherever a string is used </li></ul><ul><li>More flexible, can add, insert or append new contents, whereas the value of a String object is fixed once the string is created. </li></ul><ul><li>StringBuilder class is introduced in JDK 1.5, similar to StringBuffer but the update methods in StringBuffer are synchronized. </li></ul><ul><ul><li>StringBuilder – more efficient if accessed by single task </li></ul></ul><ul><ul><li>StringBuffer - more efficient if accessed by multiple task concurrently </li></ul></ul>
    20. 20. StringBuilder/StringBuffer classes <ul><li>public StringBuffer() </li></ul><ul><li>No characters, initial capacity 16 characters. </li></ul><ul><li>public StringBuffer(int length) </li></ul><ul><li>No characters, initial capacity specified by the length argument. </li></ul><ul><li>public StringBuffer(String str) </li></ul><ul><li>Represents the same sequence of characters as the string argument. Initial capacity 16 plus the length of the string argument. </li></ul>
    21. 21. Appending New Contents into a String Buffer <ul><li>StringBuffer strBuf = new StringBuffer(); </li></ul><ul><li>strBuf.append(&quot;Welcome&quot;); </li></ul><ul><li>strBuf.append(' '); </li></ul><ul><li>strBuf.append(&quot;to&quot;); </li></ul><ul><li>strBuf.append(' '); </li></ul><ul><li>strBuf.append(&quot;Java&quot;); </li></ul><ul><li>strBuf.insert(11, “HTML and &quot;); </li></ul><ul><li>//”Welcome to HTML and Java” </li></ul>
    22. 22. More info on StringBuffer class <ul><li>http://java.sun.com/j2se/1.3/docs/api/java/lang/StringBuffer.html </li></ul>
    23. 23. The StringTokenizer Class Constructors <ul><li>The string tokenizer class allows an application to break a string into tokens. </li></ul><ul><li>The set of delimiters (the characters that separate tokens) </li></ul>
    24. 24. The StringTokenizer Class Constructors <ul><li>StringTokenizer(String s) </li></ul><ul><ul><li>Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is &quot;  f&quot;: the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens. </li></ul></ul>
    25. 25. The StringTokenizer Class Constructors <ul><li>StringTokenizer(String s, String delim) </li></ul><ul><ul><li>Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens. </li></ul></ul>
    26. 26. The StringTokenizer Class Constructors <ul><li>StringTokenizer(String s, String delim, boolean returnTokens) </li></ul><ul><ul><li>Constructs a string tokenizer for the specified string. All characters in the delim argument are the delimiters for separating tokens. If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens. </li></ul></ul>
    27. 27. The StringTokenizer Class Methods <ul><li>boolean hasMoreTokens() </li></ul><ul><li>String nextToken() </li></ul><ul><li>String nextToken(String delim) </li></ul>
    28. 28. More infor on StringTokenizer Class <ul><li>http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html </li></ul>
    29. 29. Example 7.4 Testing StringTokenizer <ul><li>Objective: Using a string tokenizer, retrieve words from a string and display them on the console. </li></ul>TestStringTokenizer Run
    30. 30. Command-Line Parameters <ul><li>class TestMain </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ ... } </li></ul><ul><li>} </li></ul><ul><li>java TestMain arg0 arg1 arg2 ... argn </li></ul>
    31. 31. Processing Command-Line Parameters <ul><li>In the main method, get the arguments from args[0], args[1], ..., args[n] , which corresponds to arg0, arg1, ..., argn in the command line. </li></ul>
    32. 32. Example 7.5 Using Command-Line Parameters <ul><li>Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers. </li></ul>Calculator java Calculator + 2 3 java Calculator - 2 3 Run java Calculator / 2 3