格式:
字符串.replace(舊字符串,新字符串)
字符串.replaceFirst(舊字符串,新字符串) 替換第一個字符
字符串.replaceAll(舊字符串,新字符串) 替換所有字符
public class StrLen {public void replaceStr() {System.out.println(str.replace("hello","hi"));
System.out.println(str.replaceFirst("hi","你好"));
String str2 = "hello,java,hello,python";
System.out.println(str2.replaceAll("hello","hi"));
}
public static void main(String[] args) {StrLen str = new StrLen();
str.replaceStr();
}
}
輸出結(jié)果:
hi,java
你好,java
hi,java,hi,python
二、字符串比較格式:
str.equals(str2);
比較字符的長度、大小寫
public class StrLen {public void equalStr() {String str = "hello,java";
String str2 = "hello,java";
System.out.println(str.equals(str2));
}
public static void main(String[] args) {StrLen str = new StrLen();
str.equalStr();
}
}
輸出結(jié)果:
true
三、字符串查找String 類的 indexOf() 方法和 lastlndexOf() 方法用于在字符串中獲取匹配字的索引值
格式:
str.indexOf(value)
str.indexOf(value,int fromIndex)
說明:
str 表示指定字符串;value 表示要查找的字;fromIndex 表示查找時的起始索引,未說明則從第一個字符開始查找。
示例1:
String s = "Hi,Java!!";
int num = s.indexOf('v'); // num 的結(jié)果為5
示例2:
String s = "Hi,Java!!";
int num = s.indexOf('a',5); // num 的結(jié)果為
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)頁題目:Java--String字符串處理(二)-創(chuàng)新互聯(lián)
文章位置:http://www.muchs.cn/article14/sphge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、營銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、電子商務(wù)、App設(shè)計、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容