Java- String類概述

1. 字符串不變:字符串的值在建立後不能被更改。數組

String s1 = "abc"; 
s1 += "d"; 
System.out.println(s1); // "abcd" 
// 內存中有"abc","abcd"兩個對象,s1從指向"abc",改變指向,指向了"abcd"。
2. 由於String對象是不可變的,因此它們能夠被共享。 
String s1 = "abc"; 
String s2 = "abc"; 
// 內存中只有一個"abc"對象被建立,同時被s1和s2共享。
System.out.println(s1==s2);//true
3. "abc" 等效於 char[] data={ 'a' , 'b' , 'c' } 。 
// 經過字符數組構造 
char chars[] = {'a', 'b', 'c'}; 
String str2 = new String(chars);//abc
//經過字節數組構造
byte bytes[] = { 97, 98, 99 }; String str3 = new String(bytes);//abc

4.兩個字符串經過equals比較編碼

// 建立字符串對象 
String s1 = "hello"; 
String s2 = "hello"; 
String s3 = "HELLO"; 

// boolean equals(Object obj):比較字符串的內容是否相同 
System.out.println(s1.equals(s2)); // true 
System.out.println(s1.equals(s3)); // false 
System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 

//boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫
System.out.println(s1.equalsIgnoreCase(s2)); // true 
System.out.println(s1.equalsIgnoreCase(s3)); // true 
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
5.獲取功能的方法
  • public int length () :返回此字符串的長度。
  • public String concat (String str) :將指定的字符串鏈接到該字符串的末尾。
  • public char charAt (int index) :返回指定索引處的 char值。
  • public int indexOf (String str) :返回指定子字符串第一次出如今該字符串內的索引。
  • public String substring (int beginIndex) :返回一個子字符串,從beginIndex開始截取字符串到字符串結尾。
  • public String substring (int beginIndex, int endIndex) :返回一個子字符串,從beginIndex到
  • endIndex截取字符串。含beginIndex,不含endIndex。 
public static void main(String[] args) {     
    //建立字符串對象 
    String s = "helloworld"; 
    
    // int length():獲取字符串的長度,其實也就是字符個數 
    System.out.println(s.length()); 
    System.out.println("‐‐‐‐‐‐‐‐"); 
    
    // String concat (String str):將將指定的字符串鏈接到該字符串的末尾. 
    String s2 = s.concat("**hello itheima");
    System.out.println(s2);// helloworld**hello itheima 
    
    // char charAt(int index):獲取指定索引處的字符
    System.out.println(s.charAt(0)); 
    System.out.println(s.charAt(1)); 
    System.out.println("‐‐‐‐‐‐‐‐"); 
    
    // int indexOf(String str):獲取str在字符串對象中第一次出現的索引,沒有返回‐1 
    System.out.println(s.indexOf("l")); //2
    System.out.println("owo="+s.indexOf("owo")); //owo=4 索引下標是重0開始
    System.out.println(s.indexOf("ak")); 
    System.out.println("‐‐‐‐‐‐‐‐"); 
    
    // String substring(int start):從start開始截取字符串到字符串結尾 
    System.out.println(s.substring(0)); //helloworld
    System.out.println(s.substring(5)); //world  不包含5  索引下標是重1開始 不是從0開始
    System.out.println("‐‐‐‐‐‐‐‐"); 
    
    // String substring(int start,int end):從start到end截取字符串。含start,不含end。
    System.out.println(s.substring(0, s.length())); //helloworld
    System.out.println(s.substring(3,8)); //lowor
}
6.轉換功能的方法
  • public char[] toCharArray () :將此字符串轉換爲新的字符數組。
  • public byte[] getBytes () :使用平臺的默認字符集將該 String編碼轉換爲新的字節數組。
  • public String replace (CharSequence target, CharSequence replacement) :將與target匹配的字符串使用replacement字符串替換
public static void main(String[] args) { 
    //建立字符串對象 
    String s = "abcde"; 
    
    // char[] toCharArray():把字符串轉換爲字符數組 
    char[] chs = s.toCharArray(); 
    for(int x = 0; x < chs.length; x++) { 
        System.out.println(chs[x]); 
    }System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
    
    // byte[] getBytes ():把字符串轉換爲字節數組
    byte[] bytes = s.getBytes(); 
    for(int x = 0; x < bytes.length; x++) { 
        System.out.println(bytes[x]); 
    }
    System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
    
    // 替換字母it爲大寫IT
    String str = "itcast itheima"; 
    String replace = str.replace("it", "IT"); 
    System.out.println(replace); // ITcast ITheima 
    System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
7.分割功能的方法 
  • public String[] split(String regex) :將此字符串按照給定的regex(規則)拆分爲字符串數組。
public static void main(String[] args) { 
    //建立字符串對象
    String s = "aa|bb|cc"; 
    String[] strArray = s.split("|"); // ["aa","bb","cc"] 
    for(int x = 0; x < strArray.length; x++) { 
        System.out.println(strArray[x]); // aa bb cc 
    } 
}
相關文章
相關標籤/搜索