String字符串的操作

1、字符串數組與字符串之間的轉換

一個字符串可以變爲一個字符 數組,同樣,也可以把一個字符數組,變爲一個字符串。

在String類中提供了以下操作方法:

將字符串變爲字符數組:  

  • public char[] toCharArray()
  • public String(char[] value)
  • public String(char[] value,int offset,int count)

[java] view plaincopy
  1. public class StringAPIDemo01{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello" ;         // 定義字符串  
  4.         char c[] = str1.toCharArray() ; // 將一個字符串變爲字符數組  
  5.         for(int i=0;i<c.length;i++){ // 循環輸出  
  6.             System.out.print(c[i] + "、") ;   
  7.         }  
  8.         System.out.println("") ;        // 換行  
  9.         String str2 = new String(c) ;   // 將全部的字符數組變爲String  
  10.         String str3 = new String(c,0,3) ;   // 將部分字符數組變爲String  
  11.         System.out.println(str2) ;      // 輸出字符串  
  12.         System.out.println(str3) ;      // 輸出字符串  
  13.     }  
  14. };  

2、從字符串中取出指定位置的字符

如果要想使用此操作,則肯定此方法的返回值是一個char類型。pbulic charAt(int index)

[java] view plaincopy
  1. public class StringAPIDemo02{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello" ;         // 定義String對象  
  4.         System.out.println(str1.charAt(3)) ;    // 取出字符串中第四個字符  
  5.     }  
  6. };  

3、字符串與byte數組的轉換

byte數組(字節數組),在一般的IO操作中會經常使用到。

在String類中提供了以下的方法可以進行字符串與字節數組間的轉換:

  • 字符串變爲字節數組:public byte[] getBytes()
  • 將一個字節數組變爲字符串:  
    • 將全部字節數組變爲String:pbulic String(byte[] bytes)
    • 將部分字節數組變爲String:public String(byte[] bytes,int offset,int length)

[java] view plaincopy
  1. public class StringAPIDemo03{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello" ;         // 定義字符串  
  4.         byte b[] = str1.getBytes() ;    // 將字符串變爲byte數組  
  5.         System.out.println(new String(b)) ; // 將全部的byte數組變爲字符串  
  6.         System.out.println(new String(b,1,3)) ; // 將部分的byte數組變爲字符串  
  7.     }  
  8. };  

4、取得一個字符串的長度

要想取得字符串的長度:public int length()

[java] view plaincopy
  1. public class StringAPIDemo04{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello LiXingHua" ;       // 定義字符串變量  
  4.         System.out.println("\""+str1+"\"的長度爲:"+str1.length()) ;  
  5.     }  
  6. };  

5、查找指定的字符串是否存在

在實際操作中,經常會使用到判斷一個字符串中是否存在某些內容,此時就可以用以下的方法:

  • 從頭開始查找:public int indexOf(String str)
  • 從指定位置開始查找:public int indexOf(String str,int fromIndex)
查找的時候方法的返回值是一個int類型的數據,此數據表示一個字符串的具體位置,如果沒有查找到此字符串,則返回 “-1”。

[java] view plaincopy
  1. public class StringAPIDemo05{  
  2.     public static void main(String args[]){  
  3.         String str1 = "abcdefgcgh" ;                // 聲明字符串  
  4.         System.out.println(str1.indexOf("c")) ;     // 查到返回位置  
  5.         System.out.println(str1.indexOf("c",3)) ;   // 查到返回位置,從第4個位置開始查找  
  6.         System.out.println(str1.indexOf("x")) ;     // 沒有查到返回-1  
  7.     }  
  8. };  

6、去掉空格

如果假設一些信息是由用戶輸入的話,則就有可能出現多餘的空格,在這種操作中就可以使用trim()

去掉字符串的左右空格,但是字符串中間的空格是不可能去掉的。

[java] view plaincopy
  1. public class StringAPIDemo06{  
  2.     public static void main(String args[]){  
  3.         String str1 = "    hello    " ;     // 定義字符串  
  4.         System.out.println(str1.trim()) ;   // 去掉左右空格後輸出  
  5.     }  
  6. };  

7、字符截取

從一個指定的字符串中取出裏面的部分內容,使用的方法:

  • 從指定位置開始一直截取到結束位置:public String substring(int beginIndex)
  • 截取指定範圍的字符串:public String substring(int beginIndex,int endIndex) 
[java] view plaincopy
  1. public class StringAPIDemo07{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello world" ;       // 定義字符串  
  4.         System.out.println(str1.substring(6)) ; // 從第7個位置開始截取  
  5.         System.out.println(str1.substring(0,5)) ; // 截取0~5個位置的內容  
  6.     }  
  7. };  

8、拆分字符串

如果現在需要按指定的字符串去拆分一個字符串的話,但使用:public String[] split(String regex)

[java] view plaincopy
  1. public class StringAPIDemo08{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello world" ;       // 定義字符串  
  4.         String s[] = str1.split(" ") ;      // 按空格進行字符串的拆分  
  5.         for(int i=0;i<s.length;i++){     // 循環輸出  
  6.             System.out.println(s[i]) ;  
  7.         }  
  8.     }  
  9. };  

9、大小寫轉換

此功能在一般的開發語言都會存在,將一個大寫的字符串全部字母變爲小寫或者將一個小寫的字符串中的全部字母變爲大寫:

  • 小寫變大寫:public String toUpperCase()
  • 大寫變小寫:public String toLowerCase()
[java] view plaincopy
  1. public class StringAPIDemo09{  
  2.     public static void main(String args[]){  
  3.         System.out.println("將\"hello world\"轉成大寫:" + "hello world".toUpperCase()) ;  
  4.         System.out.println("將\"HELLO WORLD\"轉成小寫:" + "HELLO WORLD".toLowerCase()) ;  
  5.     }  
  6. };  

10、判斷是否以指定的字符串開頭或結尾

在String中可以使用以下兩種方法完成:

  • 判斷是否以指定的字符串開頭:public boolean startsWith(String prefix)
  • 判斷是否以指定的字符串結尾:public boolean endsWith(String suffix)
[java] view plaincopy
  1. public class StringAPIDemo10{  
  2.     public static void main(String args[]){  
  3.         String str1 = "**HELLO" ;           // 定義字符串  
  4.         String str2 = "HELLO**" ;           // 定義字符串  
  5.         if(str1.startsWith("**")){          // 判斷是否以“**”開頭  
  6.             System.out.println("(**HELLO)以**開頭") ;  
  7.         }  
  8.         if(str2.endsWith("**")){            // 判斷是否以“**”結尾  
  9.             System.out.println("(HELLO**)以**結尾") ;  
  10.         }  
  11.     }  
  12. };  

11、不區分大小寫的比較

在String類中equals()方法是可以用來進行字符串比較的,但是此種比較方法只能針對於大小寫完全一樣的字符串進行比較,如果現在要是想進行不區分大小寫的比較,則可以使用以下的方法:

  • public boolean equalsGnoreCase(String anotherString)
[java] view plaincopy
  1. public class StringAPIDemo11{  
  2.     public static void main(String args[]){  
  3.         String str1 = "HELLO" ;         // 定義字符串  
  4.         String str2 = "hello" ;         // 定義字符串  
  5.         System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;  
  6.         System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "  
  7.                 + str1.equalsIgnoreCase(str2)) ;    // 不區分大小寫的比較  
  8.     }  
  9. };  

12、字符串替換功能

在String類中提供了以下的方法用於字符串的替換操作:

  • public String replaceAll(String regex, String replacement)
[java] view plaincopy
  1. public class StringAPIDemo12{  
  2.     public static void main(String args[]){  
  3.         String str = "hello" ;          // 定義字符串  
  4.         String newStr = str.replaceAll("l","x") ;   // 現在將所有的l替換成x  
  5.         System.out.println("替換之後的結果:" + newStr) ;  
  6.     }  
  7. };  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章