Java 快速導覽 - String 類別的 replace()
String 類別 (class) 有 replace() 方法 (method) ,可置換字串 (string) 中的字元或子字串
舉例如下
編譯後執行,結果如下

http://pydoing.blogspot.tw/2011/01/java-replace.html
| 方法 | 描述 | 
|---|---|
| String replace(char oldChar, char newChar) | 將 oldChar 字元以 newChar 置換 | 
| String replace(CharSequence target, CharSequence replacement) | 將 target 子字串以 replacement 置換 | 
舉例如下
| 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 | classReplaceDemo {    publicstaticvoidmain(String[] args) {        String a = "Imagination is more important than knowledge.";        String b = "He who has hope has everything.";        String c = "台上一分鐘,台下十年功。";                System.out.println(a.replace('i', 'z'));        System.out.println(b.replace('h', 'o'));        System.out.println(c.replace("十年功", "心血酸"));    }}/* 《程式語言教學誌》的範例程式    檔名:ReplaceDemo.java    功能:示範物件導向的基本觀念     作者:張凱慶    時間:西元 2010 年 10 月 */ | 
編譯後執行,結果如下

| 中英文術語對照 | |
|---|---|
| 類別 | class | 
| 方法 | method | 
| 字串 | string | 
 
沒有留言:
張貼留言