Chapter 7 String


Published on

Chapter 7 String
Taught by Oum Saokosal, Head of Information Technology, National Polytechnic Institute of Cambodia

Published in: Technology, Education
1 Like
  • Be the first to comment

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

No notes for slide

Chapter 7 String

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