JAVA中STRING的常用方法小結_java

來源:互聯網
上載者:User

一、建立並初始化一個字串

String b = "hello";

使用構造方法建立並初始化一個字串

String();//初始化字串,表示Null 字元序列

String(value);//利用已存在的字串常量建立一個新的對象

String (char[] value);//利用一個字元數組建立一個字串

String(char[] value,int offset,int count);//截取字元數組offset到count的字元建立一個非空串

String(StringBuffer buffer);//利用StringBuffer對象初始化String對象

二、String類主要方法的使用:

1、擷取長度 *.length();//這與數組中的擷取長度不同,*.length;

2、比較字串(1) equals() //判斷內容是否相同

(2)compareTo() //判斷字串的大小關係

(3)compareToIgnoreCase(String int) //在比較時忽略字母大小寫

(4)== //判斷內容與地址是否相同

(5)equalsIgnoreCase() //忽略大小寫情況下判斷內容是否相同

如果想對字串中的部分內容是否相同進行比較,可以用

(6)reagionMatches() //有兩種 public boolean regionMatches(int toffset, String other,int ooffset,int len);表示如果String對象的一個子字串與參數other的一個子字串是相同的字元序列,則為true.要比較的String 對象的字串從索引toffset開始,other的字串從索引ooffset開始,長度為len。

public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//用布爾類型的參數指明兩個字串的比較是否對大小寫敏感。

三、尋找字串中某個位置的字元

public char charAt(int index);//返回指定索引index位置上的字元,索引範圍從0開始

四、尋找指定字串在字串中第一次或最後一詞出現的位置

在String類中提供了兩種尋找指定位置的字串第一次出現的位置的方法

(1)public int indexOf(String str);//從字串開始檢索str,並返回第一次出現的位置,未出現返回-1

(2)public int indexOf(String str,int fromIndex);//從字串的第fromIndex個字元開始檢索str

尋找最後一次出現的位置有兩種方法

(1)public int lastIndexOf(String str);

(2)public int lastIndexOf(String str,int fromIndex);

如果不關心字串的確切位置則可使用public boolean contains(CharSequence s);

五、檢查字串的起始字元和結束字元

開始的字串兩種方法

(1)public boolean starWith(String prefix,int toffset);//如果參數prefix表示的字串序列是該對象從索引toffset處開始的子字串,則返回true

(2)public boolean starWith(String prefix);

結束的字串方法

public boolean endsWith(String suffix);

六、截取子串

(1)public String subString(int beginIndex);

(2)public String subString(int beginIndex,int endIndex);//返回的字串是從beginIndex開始到endIndex-1的串

要返回後4位可以這樣寫Syetem.out.println(*.subString()(*.length()-4));

七、字串的替換

兩種方法

(1)public String replace(char oldChar,char newChar);

(2)public String replace(CharSequence target,CharSequence replacement);//把原來的etarget子序列替換為replacement序列,返回新串

(3)public String replaceAll(String regex,String replacement);//用Regex實現對字串的匹配

八、字串的大小寫替轉換

(1)public String toLowerCase(Locale locale);

(2)public String toLowerCase();

(3)public String toupperCase(Locale locale);

(4)public String toUpperCase();

九、去除字串首尾空格

*.trim();

十、字串轉換

1、將字串轉換成字元數組

public char[] toCharArray();

2、將字串轉換成字串數組

public String[] split(String regex);//regex 是給定的匹配

3、將其它資料類型轉化為字串

(1)public static String valueOf(boolean b);

(2)public static String valueOf(char c);

(3)public static String valueOf(int i);

(4)public static String valueOf(long i);

(5)public static String valueOf(float f);

(6)public static String valueOf(double d);

(7)public static String valueOf(char[] data);

(8)public static String valueOf(Object obj);

可變字串的建立和初始化

兩種方法:

public StringBuffer();

public StringBuffer(int caoacity);

StringBuffer類主要方法的使用:

一、擷取可變字串長度

(1)public int length();

(2)public int capacity();

(3)public void setLength(int newLength);

二、比較可變字串

用String 類的equals()方法比較,但是不同。

類Object中的equals()方法比較的是兩個對象的地址是否相等,而不僅僅是比較內容,但是String類在繼承Object類的時候重寫了equals()方法,只是比較兩個對象的內容是否相等

而在StringBuffer類中沒有重寫Object類的equals()方法,所以比較的是地址和內容。

三、追加和插入字串

(1)追加 public StringBuffer append(type t);

(2)插入 public StringBuffer insert(int offset,type t);//在offset處加入類型為type的字串

四、反轉和刪除字串

(1)反轉 public StringBuffer reverse();

(2)刪除 public StringBuffer delete(int start,int end);

五、減少用於可變字元序列的儲存空間

public void trimToSize();

六、StringBuffer類轉換成String類

public String toString();

聯繫我們

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