Training Java - Lesson3

501 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
501
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Training Java - Lesson3

  1. 1. Lesson2: String in Java <ul><li>What is String? </li></ul><ul><li>When use String? </li></ul><ul><li>How to be a string & How a string can be? </li></ul><ul><li>Core methods </li></ul>
  2. 2. What is String <ul><li>String str = “ 0123456789 ”; </li></ul><ul><li>String str = “ abcxyz ”; </li></ul><ul><li>String str = “ !@^%^T#(# ”; </li></ul><ul><li>String str = “ 123abc#%* ”; </li></ul><ul><li>String str = “ ’ ”; </li></ul><ul><li>String str = “ ” ”; </li></ul><ul><li>String str = “”; </li></ul><ul><li>String str = null; </li></ul>
  3. 3. When use String? <ul><li>Display every values </li></ul><ul><ul><li>System.out.println(“123abc!@#”); </li></ul></ul><ul><ul><li>System.out.println(“This is a string”); </li></ul></ul><ul><li>Covert data types </li></ul><ul><ul><li>String str = Integer.toString(10); </li></ul></ul><ul><li>Whenever you need to work with string </li></ul><ul><ul><li>In many times, you need to work with string. </li></ul></ul>
  4. 4. How to be a string <ul><li>How to be a string </li></ul><ul><ul><li>Using method toString(). In Java, every objects have this method. </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>String i = Integer.toString(10); </li></ul></ul></ul><ul><ul><ul><li>int i = 10; String s = i+” ”; </li></ul></ul></ul><ul><ul><ul><li>String s = “123abc!@#”; </li></ul></ul></ul><ul><ul><ul><li>String s = “”; </li></ul></ul></ul><ul><ul><ul><li>String s = null; </li></ul></ul></ul>
  5. 5. How a string can be <ul><li>String s = “123”; </li></ul><ul><li>int i = Integer.parseInt(s); </li></ul><ul><li>String str = “Hello,World!”; </li></ul><ul><li>String[] array_str = str.split(“,”); </li></ul><ul><li>//the result will be: </li></ul><ul><li>array_str[0] = “Hello” </li></ul><ul><li>array_str[1] = “World” </li></ul>
  6. 6. Core methods <ul><li>toUperCase() </li></ul><ul><ul><li>“ abc” -> “ABC” </li></ul></ul><ul><ul><li>“ Abc” -> “ABC” </li></ul></ul><ul><li>toLowerCase() </li></ul><ul><ul><li>“ ABC” -> “abc” </li></ul></ul><ul><ul><li>“ Abc” -> “abc” </li></ul></ul>
  7. 7. Core methods <ul><li>substring(int beginIndex) </li></ul><ul><ul><li>String str = “123abc”; </li></ul></ul><ul><ul><li>str.substring(2); -> str = “3abc” </li></ul></ul><ul><ul><li>str.substring(0); -> str = “123abc” </li></ul></ul><ul><li>substring(int beginIndex, int endIndex) </li></ul><ul><li>str.substring(0,5) -> str = “123ab’; </li></ul><ul><li>str.substring(0,str.length()) -> str=“123abc” </li></ul><ul><li>str.substring(2,4) -> str=“3ab”; </li></ul>
  8. 8. Core methods <ul><li>replaceFirst(String regex, String replacement) </li></ul><ul><ul><li>String str = “123123abc”; </li></ul></ul><ul><ul><li>str = str.replaceFirst(“123”, “321”); -> str = “321123abc”; </li></ul></ul><ul><li>replaceAll(String regex, String replacement) </li></ul><ul><ul><li>String str = “123123abc”; </li></ul></ul><ul><ul><li>str = str.replaceAll(“123”, “321”); -> str = “321321abc”; </li></ul></ul>
  9. 9. Core methods <ul><li>split(String regex) </li></ul><ul><ul><li>String str = “123-abc-*#$” </li></ul></ul><ul><ul><li>str.split(“-”) -> array{“123”,”abc”,”*#$”}; </li></ul></ul><ul><li>split(String regex, int limit) </li></ul><ul><ul><li>str.split(“-”, 5) -> array{“123”,”abc-*#$”} </li></ul></ul>

×