Java--22---String類

來源:互聯網
上載者:User

標籤:java   對象   類   string   

在Java中用於描述字串的類就是String。

Java程式中的所有字串字面值(如“abc”)都作為此類的執行個體實現。

字串最大的一個特點就是:字串是常量,它們的值在建立之後不能更改。字串緩衝區支援可變的字串。



public class sss {public static void main(String[] args) {// TODO Auto-generated method stubString s1 = "abc";s1 = "sss";System.out.println(s1);}}

列印結果卻是:sss

但是我們要明白的是,變化的不是abc 變化的是s1所指向的對象。


public class sss {public static void main(String[] args) {// TODO Auto-generated method stubString s1 = "abc";String s2 = new String("abc");System.out.println(s1==s2);System.out.println(s1.equals(s2));}}


String類複寫了Object類中的equals方法。該方法用於判斷字串內容是否相同。

 

S1和s2有什麼區別?

s1在記憶體中有一個對象,s2在記憶體中有兩個對象


public class sss {public static void main(String[] args) {// TODO Auto-generated method stubString s1 = "abc";String s2 = new String("abc");String s3 = "abc";System.out.println(s1==s2);System.out.println(s1==s3);}}


列印結果:

False

True

S3在初始化的時候發現字串“abc”已經在常量池中存在就不會再進行建立,所以s1和s3指向的是同一個對象。

 

字串常見的方法:

1.擷取

a) 字串中包含的字元數(長度)  int length ()

b) 根據位置擷取位置上的某個字元 char charAt(int index)

c) 根據字元擷取該字元的位置 

d) 

int indexOf (char ch) 返回的是ch在字串中第一次出現的位置

int indexOf (String ch):返回的是ch在字串中第一次出現的位置

int indexOf (int ch,int fromIndex):從fromIndex指定位置開始,擷取ch在字串中的位置

int indexOf (String ch,int fromIndex):從fromIndex位置開始,擷取ch在字串中出現的位置

反向索引一個字元出現的位置。int indexOf (int ch)

 

2.判斷

a) 字串中是否包含某一個子串 boolean contains (str) 特殊之處:indexOf(str):可以索引str第一次出現位置,如果返回-1,則表示該str不在字串中存在。

b) 字串中是否有內容 boolean isEmpty ()

c) 字串是否以指定內容開頭 boolean startsWith (str)

d) 字串是否以指定內容結束  boolean endsWith (str)

e) 判斷字串的內容是否相同 equals()

f) 判斷內容是否相同並忽略大小寫 equalsIgnoreCase()

3.轉換

a) 將字元數群組轉換成字串

構造方法:

String(char[] )  

String (char[] ,offset ,count)將字元數組中的一部分轉換為字串

靜態方法:

Static String copyValueOf (char[])

Static String copyValueOf (char[] data,int offset,int count)

Static String ValueOf (char[])

b) 將字串轉換成字元數組

Char[] toCharArray ()

c) 將位元組數群組轉換成字串

String(byte[] )  

String (byte[] ,offset ,count)

d) 將字串轉換成位元組數組

Byte[] getBytes()

e) 將基礎資料型別 (Elementary Data Type)轉成字串

Static String ValueOf (int)

Static String ValueOf (double)

字串和位元組數組在轉換過程中是可以指定編碼錶的。

4.替換

String replace ( oldchar ,newchar)如果要替換的不存在,返回的是原串

 

5.切割

String split(regex)

6.子串,擷取字串中的一部分

String substring(begin)從指定位置開始到結尾,如果角標不存在會出現角標越界異常

String substring(begin,end)

7.轉換 去除空格 比較

a) 將字串轉成大寫或者小寫

Sting toUpperCase()

Strign toLowerCase()

b) 將字串兩端的多個空格去除

String trim ()

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

int compareTo (string)








Java--22---String類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.