Java基礎之String類

  一、String型字符串就是多個char字符組成的一串數據。 能夠將它和字符數組進行相互轉換。 數組

  二、字符串的聲明:spa

    1)、String 變量名 = 「」;//直接聲明並賦值指針

    2)、String 變量名 = new String("");code

    3)、String 變量名;     變量名 = 「」;對象

  三、字符串的特色:blog

    1)、字符串一旦被賦值就不能改變:此處的改變說的是字符串的內容不能被改變,而不是引用不能改變。索引

      2)、賦的值做爲字符串對象和經過構造方法建立對象的方法不一樣:String s = new String("hello");和String s = "hello";是有區別的。字符串

  四、字符串之間的比較:get

    字符串的判斷方法有:string

      1)、boolean equals(Object obj):此方法比較字符串的內容是否相同,區分大小寫;

      2)、boolean equalsIngoreCase(String str):此方法也是比較字符串內容是否相同,不區分大小寫;

      3)、boolean contains(String str):判斷大字符串中是否包含小字符串;

      4)、 boolean startsWith(String str):判斷字符串是否以某個指定的字符串開頭;

      5)、 boolean endsWith(String str):判斷字符串是否以某個指定的字符串結尾;

      6)、boolean isEmpty():判斷字符串是否爲空。空字符串的表達方式有:String str = "";或者String str = null;兩種方式

    具體能夠參考下面的代碼示例:

 1 public class StringDemo {  2     public static void main(String[] args) {  3         // 建立字符串對象
 4         String s1 = "helloworld";  5         String s2 = "helloworld";  6         String s3 = "HelloWorld";  7 
 8         // boolean equals(Object obj):比較字符串的內容是否相同,區分大小寫
 9         System.out.println("equals:" + s1.equals(s2)); 10         System.out.println("equals:" + s1.equals(s3)); 11         System.out.println("-----------------------"); 12 
13         // boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫
14         System.out.println("equals:" + s1.equalsIgnoreCase(s2)); 15         System.out.println("equals:" + s1.equalsIgnoreCase(s3)); 16         System.out.println("-----------------------"); 17 
18         // boolean contains(String str):判斷大字符串中是否包含小字符串
19         System.out.println("contains:" + s1.contains("hello")); 20         System.out.println("contains:" + s1.contains("hw")); 21         System.out.println("-----------------------"); 22 
23         // boolean startsWith(String str):判斷字符串是否以某個指定的字符串開頭
24         System.out.println("startsWith:" + s1.startsWith("h")); 25         System.out.println("startsWith:" + s1.startsWith("hello")); 26         System.out.println("startsWith:" + s1.startsWith("world")); 27         System.out.println("-----------------------"); 28 
29         // 練習:boolean endsWith(String str):判斷字符串是否以某個指定的字符串結尾這個本身玩 30 
31         // boolean isEmpty():判斷字符串是否爲空。
32         System.out.println("isEmpty:" + s1.isEmpty()); 33 
34         String s4 = ""; 35         String s5 = null; 36         System.out.println("isEmpty:" + s4.isEmpty()); 37         // NullPointerException 38         // s5對象都不存在,因此不能調用方法,空指針異常
39         System.out.println("isEmpty:" + s5.isEmpty()); 40  } 41 }

 

    字符串中的"=="和equals區別,具體可參考下面的代碼片斷:

  

 1        ==和equals()  2         String s1 = new String("hello");  3         String s2 = new String("hello");  4         System.out.println(s1 == s2);// false
 5         System.out.println(s1.equals(s2));// true
 6 
 7         String s3 = new String("hello");  8         String s4 = "hello";  9         System.out.println(s3 == s4);// false
10         System.out.println(s3.equals(s4));// true
11 
12         String s5 = "hello"; 13         String s6 = "hello"; 14         System.out.println(s5 == s6);// true
15         System.out.println(s5.equals(s6));// true 

    

   因爲字符串能夠看作是多個字符組成的數組,故能夠對字符串進寫插入、獲取長度、獲取自定位置的字符等操做,具體能夠操做的功能有:

    1)、int length():獲取字符串的長度;

    2)、har charAt(int index):按照索引值得到字符串中的指定字符。Java規定,字符串中第一個字符的索引值是0,第二個字符的索引值是1,依次類推;

    3)、int indexOf(int ch):返回指定字符在此字符串中第一次出現處的索引;爲何這裏是int類型,而不是char類型? 緣由是:'a'和97其實均可以表明'a'

    4)、int indexOf(String str):返回指定字符串在此字符串中第一次出現處的索引;

    5)、int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置後第一次出現處的索引;

    6)、int indexOf(String str,int fromIndex):返回指定字符串在此字符串中從指定位置後第一次出現處的索引;

    7)、String substring(int start):從指定位置開始截取字符串,默認到末尾;

    8)、String substring(int start,int end):從指定位置開始到指定位置結束截取字符串。

   具體能夠參考下面的代碼示例:

 1 public class StringDemo {  2     public static void main(String[] args) {  3         // 定義一個字符串對象
 4         String s = "helloworld";  5 
 6         // int length():獲取字符串的長度。
 7         System.out.println("s.length:" + s.length());  8         System.out.println("----------------------");  9 
10         // char charAt(int index):獲取指定索引位置的字符
11         System.out.println("charAt:" + s.charAt(7)); 12         System.out.println("----------------------"); 13 
14         // int indexOf(int ch):返回指定字符在此字符串中第一次出現處的索引。
15         System.out.println("indexOf:" + s.indexOf('l')); 16         System.out.println("----------------------"); 17 
18         // int indexOf(String str):返回指定字符串在此字符串中第一次出現處的索引。
19         System.out.println("indexOf:" + s.indexOf("owo")); 20         System.out.println("----------------------"); 21 
22         // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置後第一次出現處的索引。
23         System.out.println("indexOf:" + s.indexOf('l', 4)); 24         System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
25         System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
26         System.out.println("----------------------"); 27 
28         // 本身練習:int indexOf(String str,int 29         // fromIndex):返回指定字符串在此字符串中從指定位置後第一次出現處的索引。 30 
31         // String substring(int start):從指定位置開始截取字符串,默認到末尾。包含start這個索引
32         System.out.println("substring:" + s.substring(5)); 33         System.out.println("substring:" + s.substring(0)); 34         System.out.println("----------------------"); 35 
36         // String substring(int start,int 37         // end):從指定位置開始到指定位置結束截取字符串。包括start索引可是不包end索引
38         System.out.println("substring:" + s.substring(3, 8)); 39         System.out.println("substring:" + s.substring(0, s.length())); 40  } 41 }

 

   String的轉換功能有:

    1)、byte[] getBytes():把字符串轉換爲字節數組;

    2)、char[] toCharArray():把字符串轉換爲字符數組;

    3)、static String valueOf(char[] chs):把字符數組轉成字符串;

    4)、static String valueOf(int i):把int類型的數據轉成字符串;注意:String類的valueOf方法能夠把任意類型的數據轉成字符串

    5)、String toLowerCase():把字符串轉成小寫;

    6)、String toUpperCase():把字符串轉成大寫;

    7)、String concat(String str):把字符串拼接。

  具體用法可參考下面的示例代碼:

 1 public class StringDemo {  2     public static void main(String[] args) {  3         // 定義一個字符串對象
 4         String s = "JavaSE";  5 
 6         // byte[] getBytes():把字符串轉換爲字節數組。
 7         byte[] bys = s.getBytes();  8         for (int x = 0; x < bys.length; x++) {  9  System.out.println(bys[x]); 10  } 11         System.out.println("----------------"); 12 
13         // char[] toCharArray():把字符串轉換爲字符數組。
14         char[] chs = s.toCharArray(); 15         for (int x = 0; x < chs.length; x++) { 16  System.out.println(chs[x]); 17  } 18         System.out.println("----------------"); 19 
20         // static String valueOf(char[] chs):把字符數組轉成字符串。
21         String ss = String.valueOf(chs); 22  System.out.println(ss); 23         System.out.println("----------------"); 24 
25         // static String valueOf(int i):把int類型的數據轉成字符串。
26         int i = 100; 27         String sss = String.valueOf(i); 28  System.out.println(sss); 29         System.out.println("----------------"); 30 
31         // String toLowerCase():把字符串轉成小寫。
32         System.out.println("toLowerCase:" + s.toLowerCase()); 33         System.out.println("s:" + s); 34         // System.out.println("----------------"); 35         // String toUpperCase():把字符串轉成大寫。
36         System.out.println("toUpperCase:" + s.toUpperCase()); 37         System.out.println("----------------"); 38 
39         // String concat(String str):把字符串拼接。
40         String s1 = "hello"; 41         String s2 = "world"; 42         String s3 = s1 + s2; 43         String s4 = s1.concat(s2); 44         System.out.println("s3:"+s3); 45         System.out.println("s4:"+s4); 46  } 47 }

  String型其餘功能有:

    1)、替換功能:String replace(char old,char new);或者String replace(String old,String new);

    2)、去除字符串兩空格:String trim();

    3)、contains() 方法用來檢測字符串是否包含某個子串

 

     4)、replace()以指定字符串做爲分隔符,對當前字符串進行分割,分割的結果是一個數組

 

  具體可參考下面的示例代碼:

 1 public class StringDemo {  2     public static void main(String[] args) {  3         // 替換功能
 4         String s1 = "helloworld";  5         String s2 = s1.replace('l', 'k');  6         String s3 = s1.replace("owo", "ak47");  7         System.out.println("s1:" + s1);  8         System.out.println("s2:" + s2);  9         System.out.println("s3:" + s3); 10         System.out.println("---------------"); 11 
12         // 去除字符串兩空格
13         String s4 = " hello world  "; 14         String s5 = s4.trim(); 15         System.out.println("s4:" + s4 + "---"); 16         System.out.println("s5:" + s5 + "---"); 17 
18         // 按字典順序比較兩個字符串
19         String s6 = "hello"; 20         String s7 = "hello"; 21         String s8 = "abc"; 22         String s9 = "xyz"; 23         System.out.println(s6.compareTo(s7));// 0
24         System.out.println(s6.compareTo(s8));// 7
25         System.out.println(s6.compareTo(s9));// -16
26  } 27 }

 

1 public class Demo { 2     public static void main(String[] args){ 3         String str = "wei_xue_yuan_is_good"; 4         String strArr[] = str.split("_"); 5  System.out.println(Arrays.toString(strArr)); 6  } 7 } 8 運行結果: 9 [wei, xue, yuan, is, good]
相關文章
相關標籤/搜索