Java String類

來源:互聯網
上載者:User

標籤:

 

首先,有兩個問題:

  1. String a = "abc";  
  2. String b = "abc";  
  1. String a = new String("abc");  
  2. String b = "abc";   a=b? a和b是否相等?為什嗎?

 分析:

1. 首先String不屬於8種基礎資料型別 (Elementary Data Type),String是一個對象。因為對象的預設值是null,所以String的預設值也是null;但它又是一種特殊的對象,有其他對象沒有的一些特性。

2. new String()和new String("")都是聲明一個新的Null 字元串,是空串不是null;

 

String類適用於描述字串事物,那麼它就提供了多個方法對字串進行操作。    

常見的操作有哪些?    

1.擷取       

1.1 字串中包含的字元數,也就是字串的長度           

int length()              

1.2 根據位置擷取位置上某個字元。           

char charAt(int index)                  

1.3 根據字元擷取該字元在字串中位置。           

int indexOf(int ch)    //返回該字元在字串中第一次出現的位置           

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

int indexOf(String str)    //返回str在字串中第一次出現的位置           

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

int lastIndexOd(int ch)         

2.判斷       

2.1 字串中是否包含某一個子串。           

boolean contains(str);           

特殊之處:indexOf(str):       

2.2 字串中是否有內容           

boolean isEmppty();       

2.3 字串是否以指定內容開頭           

boolean startWith(str);       

2.4字串是否以指定內容結尾         

 boolen endWith(str);       

2.5判斷字串內容是否相同         

 boolean equals(str);       

2.6判斷字串內容是否相同並忽略大小寫         

 boolean equalsIgnoreCase();             

3.轉換       

3.1 將字元數組轉成字串           

建構函式:    

Strnig(char[])                     

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

靜態方法:    

static String copyValueof(char[]);                       

static String copyValueof(char[], offset, count);                                              

static String valueOf(char());                              

3.2 將字串轉成字元數組               

char[] toCharArray();              

3.3 將位元組數組轉成字串               

Strnig(byte[])               

String(byte[], offset, count); 將位元組數組中的一部分轉成字串                      

3.4將字串轉成位元組數組               

byte[] getBytes();              

3.5將基礎資料型別 (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. 轉換     

7.1 將字串轉成大寫或小寫           

String toUpperCase();           

String toLowerCase();              

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

 String trim();                  

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

int compareTo(string);

 

 

Java 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.