Java字串詳解的執行個體介紹

來源:互聯網
上載者:User

1.建立對象

對於java程式中的字串直接常量,JVM會使用一個字串池來儲存它們。當第一次使用某個字串直接常量時,JVM會將它放入字串池中進行緩衝。在一般情況下,字串池中的字串對象不會被記憶體回收。當程式再次需要使用該字串時,無需重新建立一個新的字串就可以直接讓引用變數直接指向字串中已有的字串。而使用new操作建立的字串對象不指向字串池中的對象,但是可以使用intern()方法使其指向字串池中的對象。複製代碼 代碼如下:public class StringDemo1 {
public static void main(String[] args){
String str1 ="abc";
String str2 ="abc";
String str3 =new String("abc");
System.out.println(str1==str2);//true
System.out.println(str1==str3);//false
}
}

常見問題
複製代碼 代碼如下:String str3 =new String("abc");

建立了幾個對象?
答:兩個
複製代碼 代碼如下:String str ="ab"+"cd";

建立了幾個對象?

答:一個。"ab"和"cd"都是常量被放在字串池中。因此只建立了一個abcd字串池中並將字串abcd儲存在字串池中。

複製代碼 代碼如下:public class StringDemo1 {
public static void main(String[] args){
String str1 ="ab";
String str2 ="cd";
String str3 ="ab"+"cd";//建立對象並加入字串池
String str4 =str1+str2;
String str5 =str1+"cd";
System.out.println(str3==str4);//false
System.out.println(str3==str5);//false

}
}

由上面代碼可知:只有引號包含文本的方式才建立的String對象才能被添加到字串池中,對於包含new方法建立對象的”+“串連運算式他所產生的新對象不會被加到字串池中。

但是有一種情況需要引起我們的注意:

複製代碼 代碼如下:public class StringDemo1 {
private final static String str1 ="ab";
private final static String str2 ="cd";
public static void main(String[] args){
String str3 ="ab"+"cd";//建立對象並加入字串池
String str4 =str1+str2;
String str5 =str1+"cd";
System.out.println(str3==str4);//true
System.out.println(str3==str5);//true

}
}

這又是為什麼呢?原因是這樣的,對於常量來講。它的值是固定的,因此在編譯期間就能被確定了。

將上面的代碼稍加改變看看會出現什麼情況。

複製代碼 代碼如下:public class StringDemo1 {
private final static String str1 ;
private final static String str2;
static{
str1="ab";
str2="cd";
}
public static void main(String[] args){
String str3 ="ab"+"cd";//建立對象並加入字串池
String str4 =str1+str2;
String str5 =str1+"cd";
System.out.println(str3==str4);//false
System.out.println(str3==str5);//false

}
}

str1和str2雖然被定義為常量,但是她們美譽馬上賦值,在運算出s的值前,她們何時被賦值,以及被賦什麼值都是變數,因此性質和變數一樣。只能在運行時被建立。

2.字串方法

擷取方法

•int length()
•char charAt(int index)根據位置擷取某個字元
•int indexOf(int ch) 返回的是ch在字串中第一次出現的位置
•int indexOf(int ch,int fromIndex)從fromIndex指定位置開始,擷取ch在字串中第一次出現的位置
•int indexOf(String str)
•int indexOf(String str,int fromIndex)
•int lastIndexOf(int ch)

判斷方法

•boolean contains(String str) 另一種判斷方法:if(str.index(str)!=-1)
•boolean startsWith(String str)
•boolean endsWith(String str)
•bolean isEmpty(String str)
•boolean equals(String str)
•boolean equalsIgnoreCase(String str);

轉換方法

•將字元數群組轉換為字串

建構函式

1.String(char[] chs)

2.String(char[] chs,offset,count)將字元數組中的一部分轉成字串。

靜態方法

1.static String copyValueOf(char[] chs)

2.static String copyValueOf(char[] chs,int offset,int count)

3.static String valueOf(char[] )

4.static String valueOf(char[] chs,int offset,int count)

•將字串轉換成字元數組

char[] toCharArray

•將字元數群組轉換成字串
•將字串轉換成位元組數組
byte[] toBytes

替換方法

String replace(olderStr,newStr)

切割方法

String split(regex)

擷取子串[編輯分類]

String subString(begin)

String subString(begin,end)包含頭不包含尾

將字串轉換成大小寫Android(10)

String toUpperCase()

String toLowerCase()

將字串兩端的空格去除

String trim()

對兩個字串進行自然順序的比較

int compareTo(String str)

3.String 練習

1.字串翻轉

複製代碼 代碼如下:public class StringDemo2 {
public static void main(String[] args){
String str = "avdkfasjks";
reverseMethod_1(str);
}
public static void reverseMethod_1(String str){
for(int i=str.length();i>0;i--){
System.out.print(str.charAt(i-1));
}
}
}

2.擷取最大相同子串
複製代碼 代碼如下:public class StringDemo2 {
public static void main(String[] args){
String str1 = "avdkfasjks";
String str2 = "ewavdrtte";
System.out.println(commonMaxSubstring(str1, str2));
}
public static String commonMaxSubstring(String str1,String str2){
int len = str1.length();
String str3 = null;
outer:
//i為子串的長度
for(int i = len;i>0;i--){
//j為子串的腳標
for(int j=0;j<len-i+1;j++){
str3=str1.substring(j,j+i);
if(str2.contains(str3))
break outer;

}
}
return str3;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.