更新時(shí)間:2022-08-11 11:14:32 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1428次
比較常用的Java字符截取方法有哪些?動(dòng)力節(jié)點(diǎn)小編為大家舉例說(shuō)明。
1.length() 字符串的長(zhǎng)度
示例:
char chars[]={'a','b'.'c'};
字符串 s=新字符串(字符);
int len=s.length();
2. charAt() 截取一個(gè)字符
示例:
char ch;
ch="abc".charAt(1); 返回'b'
3. getChars() 截取多個(gè)字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定子串開始字符的下標(biāo),sourceEnd 指定子串結(jié)束后下一個(gè)字符的下標(biāo)。因此,子字符串包含從 sourceStart 到 sourceEnd-1 的字符。接收到的字符數(shù)組由目標(biāo)指定,目標(biāo)中開始復(fù)制的子字符串的下標(biāo)值為targetStart。
示例:
String s="this is a demo of the getChars method.";
字符 buf[]=新字符[20];
s.getChars(10,14,buf,0);
4. getBytes()
getChars() 的替代方法是將字符存儲(chǔ)在字節(jié)數(shù)組中,此方法是 getBytes()。
5. toCharArray()
6. equals()和equalsIgnoreCase()比較兩個(gè)字符串
7. regionMatches() 用于將字符串中的特定區(qū)域與另一個(gè)特定區(qū)域進(jìn)行比較。它有一個(gè)重載形式,允許您在比較中忽略大小寫。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
8.startsWith()和endsWith()
startsWith() 方法確定是否以特定字符串開頭, endWith() 方法確定是否以特定字符串結(jié)尾
9. 等于() 和 ==
equals() 方法比較字符串對(duì)象中的字符,== 運(yùn)算符比較兩個(gè)對(duì)象是否引用同一個(gè)實(shí)例。
示例:
字符串 s1="Hello";
字符串 s2=新字符串(s1);
s1.eauals(s2);//真
s1==s2;//假
10. compareTo() 和 compareToIgnoreCase() 比較字符串
11. indexOf() 和 lastIndexOf()
indexOf() 查找字符或子字符串的第一次出現(xiàn)。
lastIndexOf() 查找字符或子字符串最后一次出現(xiàn)的位置。
12. 子串()
它有兩種形式,第一種是:String substring(int startIndex)
第二個(gè)是:String substring(int startIndex,int endIndex)
13. concat()連接兩個(gè)字符串
14. replace()替換
它有兩種形式。第一種形式使用一個(gè)字符來(lái)替換調(diào)用字符串中出現(xiàn)的所有字符。
字符串替換(字符原始,字符替換)
例如: String s="Hello".replace('l','w');
第二種形式是將一個(gè)字符序列替換為另一個(gè)字符序列,形式如下:
字符串替換(CharSequence 原始,CharSequence 替換)
15. trim() 去掉開始和結(jié)束的空格
16. valueOf()轉(zhuǎn)換為字符串
17. toLowerCase() 轉(zhuǎn)換為小寫
18. toUpperCase() 轉(zhuǎn)換為大寫
19. StringBuffer 構(gòu)造函數(shù)
StringBuffer 定義了三個(gè)構(gòu)造函數(shù):
字符串緩沖區(qū)()
字符串緩沖區(qū)(整數(shù)大小)
字符串緩沖區(qū)(字符串字符串)
StringBuffer(CharSequence 字符)
(1)長(zhǎng)度()和容量()
一個(gè)StringBuffer的當(dāng)前長(zhǎng)度可以通過(guò)length()方法得到,整個(gè)可分配空間可以通過(guò)capacity()方法得到。
(2)ensureCapacity() 設(shè)置緩沖區(qū)的大小
無(wú)效確保容量(整數(shù)容量)
(3)setLength()設(shè)置緩沖區(qū)的長(zhǎng)度
無(wú)效集長(zhǎng)度(int len)
(4)charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)append()可以將任意類型數(shù)據(jù)的字符串表示形式連接到被調(diào)用的StringBuffer對(duì)象的末尾。
示例:
int a=42;
StringBuffer sb=new StringBuffer(40);
字符串 s=sb.append("a=").append(a).append("!").toString();
(7)insert() 插入字符串
StringBuffer 插入(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer 插入(int 索引,對(duì)象 obj)
index 指定字符串插入到 StringBuffer 對(duì)象的位置的索引。
(8)reverse() 反轉(zhuǎn)StringBuffer對(duì)象中的字符
字符串緩沖區(qū)反向()
(9)delete()和deleteCharAt()刪除字符
StringBuffer 刪除(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)replace()替換
StringBuffer 替換(int startIndex,int endIndex,String str)
(11)substring()截取子串
字符串子字符串(int startIndex)
字符串子字符串(int startIndex,int endIndex)
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)