Java String類詳解(二)

上一篇Java String類詳解(一)講了String類對象的兩種實例化方式及其區別,字符串比較,以及String類的特點,我們接着來講String類的常用方法。

一、字符串的常用方法 — 字符與字符串

很多編程語言利用了字符數組的概念來描述字符串的概念,在String類的方法上也有所體現。

1

一個例子:字符串和字符數組轉換,完成一個小寫字符串變爲大寫字符串的操作

public class StringDemo {
         public static void main(String args[]) {
                   String str = "helloworld" ;
                   char data [] = str.toCharArray() ;     // 字符串變爲字符數組
                   for (int x = 0 ; x < data.length ; x ++) {
                            System.out.print(data[x] + "、") ;
                            data [x] -= 32 ;    // 變大寫
                   }
                   System.out.println();
                   System.out.println("全部字符數組變爲字符串:" + new String(data)) ;
                   System.out.println("部分字符數組變爲字符串:" + new String(data,0,5)) ;
         }
}

運行結果:

h、e、l、l、o、w、o、r、l、d、
全部字符數組變爲字符串:HELLOWORLD
部分字符數組變爲字符串:HELLO

二、字符串的常用方法 — 字節與字符串

字節使用byte描述,字節一般用在數據的傳輸和進行編碼轉換的時候使用。String中也提供相應的方法,來進行數據傳輸和編碼轉換。

22

一個例子:完成一個小寫字母變爲大寫字母的操作

public class StringDemo {
         public static void main(String args[]) {
                   String str = "helloworld" ;
                   byte data [] = str.getBytes() ;  // 字符串變爲字節數組
                   for (int x = 0 ; x < data.length ; x ++) {
                            System.out.print(data[x] + "、") ;
                            data [x] -= 32 ;    // 變大寫
                   }
                   System.out.println() ;
                   System.out.println("全部字節數組變爲字符串:" + new String(data)) ;
                   System.out.println("部分字節數組變爲字符串:" + new String(data,0,5)) ;
         }
}

運行結果:

104、101、108、108、111、119、111、114、108、100、
全部字節數組變爲字符串:HELLOWORLD
部分字節數組變爲字符串:HELLO

三、字符串的常用方法 — 字符串比較

3

一個例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str1 = "helloworld" ;
                   String str2 = "HELLOWORLD" ;
                   System.out.println(str1.equals(str2)) ;
                   System.out.println(str1.equalsIgnoreCase(str2)) ;
         }
}

運行結果:

false
true

四、字符串的常用方法 — 字符串查找

4

一個例子:判斷開頭和結尾操作

public class StringDemo {
         public static void main(String args[]) {
                   String str = "**@@hello##" ;
                   System.out.println(str.startsWith("**")) ;
                   System.out.println(str.startsWith("@@",2)) ;
                   System.out.println(str.endsWith("##")) ;
         }
}

運行結果:

true
true
true

另外一個例子:使用contains()方法查找字符串是否存在,直接返回boolean,用於各種的執行判斷

public class StringDemo {
         public static void main(String args[]) {
                   String str = "helloworld" ;
                   System.out.println(str.contains("hello")) ;
                   System.out.println(str.contains("xx")) ;
         }
}

運行結果:

true
false

五、字符串的常用方法 — 字符串替換

5

一個例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World ." ;
                   System.out.println(str.replaceAll("l","_")) ;
                   System.out.println(str.replaceFirst("l","_")) ;
         }
}

運行結果:

He__o Wor_d .
He_lo World .

六、字符串的常用方法 — 字符串截取

6

例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World ." ;
                   System.out.println(str.substring(6)) ;       
                   System.out.println(str.substring(0,5));    
         }
}

運行結果:

World .
Hello

七、字符串的常用方法 — 字符串拆分

7

例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World !!!" ;
                   String result [] = str.split(" ") ;
                   for (int x = 0 ; x < result.length ; x ++) {
                            System.out.println(result[x]) ;
                   }
         }
}

運行結果:

Hello
World
!!!

八、字符串的常用方法 — 其他方法

8

例子1:取得字符串長度和是否爲空

public class StringDemo {
         public static void main(String args[]) {
                   String str = "hello" ;
                   System.out.println(str.isEmpty()) ;
                   System.out.println("".isEmpty()) ;
                   System.out.println(str.length()) ;
                   System.out.println("    Hello     ".length()) ;       // 空格也計算
         }
}

運行結果:

false
true
5
14

例子2:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World !~!!" ;
                   System.out.println(str.toUpperCase());
                   System.out.println(str.toLowerCase());
                   System.out.println("Hello ".concat("World ."));          // +也可以
         }
}

運行結果:

HELLO WORLD !~!!
hello world !~!!
Hello World .
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章