java string类api_JAVA中String类的常用方法API
@[toc]
前言
String 類是我們?nèi)粘=?jīng)常使用的Java類,以下是對該類的信息匯總,類的關(guān)系圖如下
String類關(guān)系圖
創(chuàng)建:
String s="hello!";//使用字符串常量直接初始化
new String();//使用構(gòu)造方法創(chuàng)建并初始化一個對象,表示空字符序列
String(value);//利用已存在的字符串常量創(chuàng)建一個新的對象
String(char[] value);//利用一個字符數(shù)組創(chuàng)建一個字符串
String(char[] value,int offset,int count);//截取字符數(shù)組offset到count的字符創(chuàng)建一個非空串
String(StringBuffer buffer);//利用StringBuffer對象初始化String對象
String類主要方法的使用:
一、獲取長度
public int length(); // 這與數(shù)組中的length屬性獲取長度不同
二、比較相等
== //判斷內(nèi)容與地址是否相同
public boolean equals(Object anObject);//判斷內(nèi)容是否相同
public boolean equalsIgnoreCase(String anotherString);//忽略大小寫的情況下判斷內(nèi)容是否相同
如果想對字符串中的部分內(nèi)容是否相同進(jìn)行比較,可以用reagionMatches() 有兩種
public boolean regionMatches(int toffset, String other,int ooffset,int len);表示如果String對象的一個子字符串與參數(shù)other的一個子字符串是相同的字符序列,則為true.要比較的String對象的字符串從索引toffset開始,other的字符串從索引ooffset開始,長度為len。
public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//用布爾類型的參數(shù)指明兩個字符串的比較是否對大小寫敏感。
三、比較大小
public int compareTo(String anotherString); //判斷字符串的大小關(guān)系
public int compareToIgnoreCase(String str); //在比較時忽略字母大小寫
四、查找字符串中某個位置的字符
public char charAt(int index);//返回指定索引index位置上的字符,索引范圍從0開始
五、查找指定字符串在字符串中第一次或最后一詞出現(xiàn)的位置
在String類中提供了兩種查找指定位置的字符串第一次出現(xiàn)的位置的方法
public int indexOf(String str);//從字符串開始檢索str,并返回第一次出現(xiàn)的位置,未出現(xiàn)返回-1
public int indexOf(String str,int fromIndex);//從字符串的第fromIndex個字符開始檢索str
查找最后一次出現(xiàn)的位置有兩種方法
public int lastIndexOf(String str);
public int lastIndexOf(String str,int fromIndex);
如果不關(guān)心字符串的確切位置則可使用public boolean contains(CharSequence s);
六、檢查字符串的起始字符和結(jié)束字符
開始的字符串兩種方法
public boolean starWith(String prefix,int toffset);//如果參數(shù)prefix表示的字符串序列是該對象從索引toffset處開始的子字符串,則返回true
public boolean starWith(String prefix);
結(jié)束的字符串方法
public boolean endsWith(String suffix);
七、截取子串
public String subString(int beginIndex);// 返回以beginIndex開頭直至該字符串結(jié)尾的串
public String subString(int beginIndex,int endIndex);//返回的字符串是從beginIndex開始到endIndex-1的串,要返回后4位可以這樣寫Syetem.out.println(*.subString()(*.length()-4));
八、字符串的替換
public String replace(char oldChar,char newChar);
public String replace(CharSequence target,CharSequence replacement);//把原來的etarget子序列替換為replacement序列,返回新串
public String replaceAll(String regex,String replacement);//用正則表達(dá)式實(shí)現(xiàn)對字符串的匹配
九、字符串的大小寫替轉(zhuǎn)換
public String toLowerCase(Locale locale);
public String toLowerCase();
public String toupperCase(Locale locale);
public String toUpperCase();
十、去除字符串首尾空格
public String trim();
十一、字符串轉(zhuǎn)換
1、將字符串轉(zhuǎn)換成字符數(shù)組
public char[] toCharArray();
2、將字符串轉(zhuǎn)換成字符串?dāng)?shù)組
public String[] split(String regex);//regex 是給定的匹配
3、將其它數(shù)據(jù)類型轉(zhuǎn)化為字符串
public static String valueOf(boolean b);
public static String valueOf(char c);
public static String valueOf(int i);
public static String valueOf(long i);
public static String valueOf(float f);
public static String valueOf(double d);
public static String valueOf(char[] data);
public static String valueOf(Object obj);
4、可變字符串的創(chuàng)建和初始化,兩種方法:
public StringBuffer();
public StringBuffer(int caoacity);
StringBuffer類主要方法的使用:
一、獲取可變字符串長度
public int length();
public int capacity();
public void setLength(int newLength);
二、比較可變字符串
用String 類的equals()方法比較,但是不同。類Object中的equals()方法比較的是兩個對象的地址是否相等,而不僅僅是比較內(nèi)容,但是String類在繼承Object類的時候重寫了equals()方法,只是比較兩個對象的內(nèi)容是否相等,而在StringBuffer類中沒有重寫Object類的equals()方法,所以比較的是地址和內(nèi)容。
三、追加和插入字符串
追加public StringBuffer append(type t);
插入 public StringBuffer insert(int offset,type t); //在offset處加入類型為type的字符串
四、反轉(zhuǎn)和刪除字符串
反轉(zhuǎn)public StringBuffer reverse();
刪除 public StringBuffer delete(int start,int end);
五、減少用于可變字符序列的存儲空間
public void trimToSize();
六、StringBuffer類轉(zhuǎn)換成String類
public String toString();
歡迎關(guān)注我的公眾號【小狐貍與小兔子】,日常分享工作、技術(shù)、學(xué)習(xí)、生活等隨筆
總結(jié)
以上是生活随笔為你收集整理的java string类api_JAVA中String类的常用方法API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 获取文件权限_Java中的文件
- 下一篇: java中引用一个文件数据_JAVA-基