java,String

很多的編程語言都會強調利用字符數組來描述字符串,實際上在Java裏面也存在有類似的概念,在String類中也提供有一系列與字符操作有關的方法。

No.

方法名稱

類型

描述

1

public String(char[] value)

構造

將全部的字符數組作爲String的內容

2

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

構造

將部分的字符數組變爲字符串,設置字符數組的開始索引與使用個數

3

public char charAt(int index)

普通

返回指定索引位置上的字符

4

public char[] toCharArray()

普通

將字符串以字符數組的形式返回

範例:驗證charAt()方法

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.    

  3.       public static void main(String args[]) {  

  4.              String str = "hello" ;  

  5.             char c = str.charAt(0) ;  

  6.             System.out.println(c) ;  

  7.        }  

  8. }  

 

在程序中字符串索引都是從0開始的。

而後最爲關鍵的是實現字符串與字符數組間的互相轉換。

範例:將字符串變爲字符數組


Java代碼  收藏代碼

  1. public class StringDemo {  

  2.        public static void main(String args[]) {  

  3.            String str = "helloworld" ;  

  4.    char [] data = str.toCharArray() ;// 將字符串變爲字符數組  

  5.           for (int x = 0 ; x < data.length ; x ++) {  

  6.       System.out.print(data[x] + "、") ;  

  7.           }  

  8.      }  

  9. }  

 

    當字符串變爲字符數組之後就可以針對於裏面的每一個字符進行操作。

範例下載將字符串中的小寫字母變爲大寫字母

· 小寫字母的編碼 大寫字母的編碼,差了32,也就是說每一個字符都減去32即可。

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "helloworld" ;  

  4.         char [] data = str.toCharArray() ;  // 將字符串變爲字符數組  

  5.         for (int x = 0 ; x < data.length ; x ++) {  

  6.             data[x] -= 32 ;         // 編碼減去32,成大寫  

  7.         }  

  8.         System.out.println(new String(data)) ;  

  9.         System.out.println(new String(data,5,5)) ;   // 第5個索引,之後取5個長度的內容  

  10.     }  

  11. }  

   下面可以利用此操作功能判斷某一個字符串是否全部由數字所組成。

 

     · 將字符串變爲字符數組,這樣做的目的可以進行每個字符的依次判斷;

     · 判斷每一個字符的範圍是否是數字:'0' ~ '9'之間。

範例:實現判斷

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "123" ;  

  4.         System.out.println(isNumber(str)) ;  

  5.     }  

  6.     public static boolean isNumber(String temp) {  

  7.         char data [] = temp.toCharArray() ;  // 將字符串變爲字符數組  

  8.         for (int x = 0 ; x < data.length ; x ++) {  

  9.             if (data[x] < '0' || data[x] > '9') {  // 不是數字  

  10.                 return false ;  

  11.             }  

  12.         }  

  13.         return true ;  

  14.     }  

  15. }  

 

 

3.2、字節與字符串

    除了字符可以與字符串進行互相轉換之外,字節也可以進行轉換,但是這樣的轉換往往會出現在實際的開發環節中。

No.

方法名稱

類型

描述

1

public String(byte[] bytes)

構造

將全部字節數組變爲字符串

2

public String(byte[] bytes, int offset, int length)

構造

將部分字節數組變爲字符串

3

public byte[] getBytes()

普通

將字符串變爲字節數組

4

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException

普通

編碼轉化

    首先對於byte一定要清楚,雖然它的範圍要比char小,但是byte還是可以明確的描述出字母的範圍的。

範例下載利用字節數組實現小寫字母變爲大寫字母的操作

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "helloworld" ;  

  4.         byte data [] = str.getBytes() ; // 將字符串變爲字節數組  

  5.         for (int x = 0 ; x < data.length ; x ++) {  

  6.             data[x] -= 32 ;  // 改變編碼  

  7.         }  

  8.         System.out.println(new String(data)) ;  

  9.     }  

  10. }  

 

 

3.3、字符串比較

現在至少應該知道了字符串比較有一個equals()方法,但是此方法本身是屬於區分大小寫的比較,爲此在Java裏面針對於字符串的比較實際上還有其他方法。

No.

方法名稱

類型

描述

1

public boolean equals(String anObject)

普通

區分大小寫的字符串比較

2

public boolean equalsIgnoreCase(String anotherString)

普通

不區分大小寫的字符串比較

3

public int compareTo(String anotherString)

普通

比較字符串大小

4

public int compareToIgnoreCase(String str)

普通

不區分大小寫比較字符串大小

範例下載觀察equals()的問題

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String strA = "hello" ;  

  4.         String strB = "HEllo" ;  

  5.         System.out.println(strA.equals(strB)) ; // false  

  6.         System.out.println(strA.equalsIgnoreCase(strB)) ;   // true  

  7.     }  

  8. }  

     驗證碼操作中可真的部分大寫或者是小寫字母。

 

     在進行字符串相等的比較操作之中,最爲關鍵的一個方法是:compareTo(),這個方法本身返回一個int型數據,而這個int型數據有三種結果:大於(>0)、小於(<0)、等於(=0)。

範例:比較字符串大小

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String strA = "A" ;  

  4.         String strB = "a" ;  

  5.         System.out.println(strA.compareTo(strB)) ;  // -32  

  6.         System.out.println(strA.compareToIgnoreCase(strB)) ;    // 0  

  7.     }  

  8. }  

     compareTo()方法之中要進行比較是依據編碼進行相減得來的。所以利用compareTo()返回值範圍進行判斷就可以區分大小關係了。

 

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String strA = "a" ;  

  4.         String strB = "a" ;  

  5.         System.out.println(strA.compareTo(strB)) ;  // -32  

  6.         System.out.println(strA.compareToIgnoreCase(strB)) ;    // 0  

  7.         if (strA.compareTo(strB) == 0) {  

  8.             System.out.println("兩個字符串的內容相同!") ;  

  9.         }  

  10.     }  

  11. }  

 

 

3.4、字符串查找

     從一個完整的字符串之中查找一些子字符串,而可以實現這種字符串查找功能的方法有如下幾個:

No.

方法名稱

類型

描述

1

public boolean contains(String s)

普通

判斷某一個字符串是否存在

2

public int indexOf(String str)

普通

取得某一個子字符串的索引位置,找不到返回-1

3

public int indexOf(String str, int fromIndex)

普通

從指定索引位置開始檢索子字符串位置,找不到就返回-1

4

public int lastIndexOf(String str)

普通

從後向前查找指定子字符串的位置,找不到返回-1

5

public int lastIndexOf(String str, int fromIndex)

普通

從指定位置由後向前查找子字符串的位置,找不到返回-1

6

public boolean startsWith(String prefix)

普通

判斷是否以某個字符串開頭

7

public boolean startsWith(String prefix, int toffset)

普通

從指定位置判斷是否以某個字符串開頭

8

public boolean endsWith(String suffix)

普通

是否以指定的字符串結尾

      如果要查找中間的內容,那麼現在基本上都使用contains()方法了。

範例下載使用contains()方法

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "hello" ;  // 0:h、1:e、2:l、3:l、4:o。  

  4.         if (str.contains("l")) {     // 此方法直接返回true或者是false  

  5.             System.out.println("已查找到!") ;  

  6.         }  

  7.     }  

  8. }  

 contains()方法雖然現在用的比較多,但是其是在JD K1.5後才提供的。而在JD 1.5之前那麼只能夠通過indexOf()方法實現。

 

範例:利用indexOf()方法判斷

 

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "hello" ;  // 0:h、1:e、2:l、3:l、4:o。  

  4.         System.out.println(str.indexOf("l")) ;  // 2,第一個滿足的位置  

  5.         if (str.indexOf("l") != -1) {   // 現在表示索引查找到了  

  6.             System.out.println("內容已經查找到!") ;  

  7.         }  

  8.     }  

  9. }  

 默認情況下indexOf()都是從第一個字母開始查找。那麼也可以利用其的重載方法從指定索引位置查找。

 

範例:其他查找

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "hello" ;  // 0:h、1:e、2:l、3:l、4:o。  

  4.         System.out.println(str.indexOf("l",3)) ;    // 3  

  5.         System.out.println(str.lastIndexOf("l")) ;  // 3  

  6.     }  

  7. }  

 在字符串查找操作裏面還可以判斷開頭或者是結尾。

範例下載判斷開頭或結尾

Java代碼  收藏代碼

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "**@@hello##" ;  

  4.         System.out.println(str.startsWith("**")) ;      // true  

  5.         System.out.println(str.startsWith("@@",2)) ;    // true  

  6.         System.out.println(str.endsWith("##")) ;    // true  

  7.     }  

  8. }  

 這幾個查詢現在出現最多的就是contains()startsWith()endsWith()


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