Chapter 7 String

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Chapter 7 String - Presentation Transcript

    1. Chapter 7 Strings Oum Saokosal , Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail: oum_saokosal@yahoo.com
    2. Strings p.257
      • Introduction
      • The String class
      • The Character class
      • The StringBuffer class
    3. Introduction
      • A string is sequence (series) of characters.
      • A string is NOT an array of characters.
        • E.g. in C/C++: char s[20];
      • In Java, A String is an object.
      • Java has 3 String classes:
        • String
        • StringBuffer
        • StringTokenizer
    4. The String class
    5. The String class (1)
      • String is in java.lang package.
      • Since java.lang.* is always imported automatically, we don’t need to import the String class.
      • Declaration:
        • String s1;
      • Initialization:
        • s1=“Information Technology”;
      • Or, short-cut:
        • String s1=“Information Technology”;
    6. The String class (2)
      • Because String is a class, then s1 is an object.
      • So there should be constructors, methods, or properties.
      • String constructors:
        • String()
        • String(String value)
        • String(char[] value)
        • Ex: String s1 = new String(“IT”);
      • You just say:
        • String s1 = “IT”;
    7. The String class (3)
      • Summary of String class
      • Constructors
        • String()
      • Methods
        • charAt()
        • compareTo()
        • concat()
        • endsWidth()
        • equals()
        • getChars()
        • equalsIgnoreCase()
        • getChars()
        • indexOf()
        • lastIndexOf()
        • regionMatches()
        • length()
        • replace()
        • startsWith()
        • subString()
        • toCharArray()
        • toLowerCase()
        • toString()
        • toUpperCase()
        • trim()
        • copyValueOf()
        • valueOf()
    8. The String class (4)
      • Q. How do I get those methods to use?
      • A. You just declare a variable as String.
        • String s1=“npic”;
        • Then, you call a method, say length() .
        • System.out.println( s1.length() );
      • Let’s try this out:
        • Public class TestString {
        • public static void main(String[] args){
        • String s1=“npic”;
        • System.out.println(s1.length());
        • }
        • }
    9. The String class (5)
      • Note:
      • The String class is immutable (has no setter method).
      • The String class is final so we cannot inherit from it. //Discuss it in chapter 8
      • Lab time:
        • Example 7.1 Checking Palindromes p.266
    10. The Character class
    11. The Character class
      • To declare a variable as a character, use primitive data type: char .
        • Ex: char ch1 = ‘a’;
      • But Java provides also Character class. It is useful for Data Structure.
        • Ex: Character ch2 = new Character(‘b’);
        • Character ch3 = ‘c’;
      • After define ch2,ch3 as Character, then these can use methods from Character class. Please see methods on page 268.
      • Lab Time:
        • Example 7.2 on page 268
    12. The StringBuffer class
    13. The StringBuffer class (1)
      • StringBuffer class is more flexible than String class. Why?
      • Because after creating a variable from StringBuffer class, we can use append, delete, insert etc. very easily.
      • Example:
        • StringBuffer sb = new StringBuffer(“NPI”);
        • sb.append(“C”);
    14. The StringBuffer class (2)
      • StringBuffer()
      • append():StringBuffer
      • capacity():int
      • charAt():char
      • delete():StringBuffer
      • deleteCharAt():StringBuffer
      • insert():StringBuffer
      • length():int
      • replace():StringBuffer
      • reverse():StringBuffer
      • setCharAt():void
      • setLength():void
      • subString():String
      Homework
    15. The StringBuffer class (2)
      • Append
        • StringBuffer st = new StringBuffer("H");
        • st.append('i');
        • st.append(5);
        • st.append(".");
        • st.append("com");
      • //output: Hi5.com
      • Please make some note on the code.
      • StringBuffer provided overloaded methods to append boolean, char, char[], double, float, int, long, String .
    16. Lab: Exercise 1
      • Count the number of words in a given String
      • Example:
        • Input: National Polytechnic Institute of Cambodia
        • Output: Word Count: 5 words
    17. Lab: Exercise 2
      • On page 285, Section 7.4 The StringBuffer Class
      • 7.11 (Sorting character in a String) Write a method that returns a sorted string using the following header:
      • public static String sort(String s)
      • For example, sort(“bac”) return abc .
    SlideShare Zeitgeist 2009

    + OUM  SAOKOSALOUM SAOKOSAL Nominate

    custom

    113 views, 1 favs, 0 embeds more stats

    Chapter 7 String
    Taught by Oum Saokosal, Head of In more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 113
      • 113 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 12
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories