共計 432 個字符,預計需要花費 2 分鐘才能閱讀完成。
replaceAll()
方法是 Java 中 String
類的一個方法,用于在字符串中替換所有匹配的子字符串。
語法:
public String replaceAll(String regex, String replacement)
參數:
regex
:要替換的子字符串的正則表達式模式。replacement
:用于替換的新子字符串。
返回值:
- 返回一個新的字符串,其中所有匹配的子字符串都被替換為新的子字符串。
使用 replaceAll()
方法時,會將字符串中所有與正則表達式匹配的子字符串都替換為指定的新字符串。
示例:
String str = "Hello, World!";
String newStr = str.replaceAll("o", "x");
System.out.println(newStr);
// 輸出:Hellx, Wxrld!
在上面的示例中,replaceAll("o", "x")
將字符串中所有的 o
替換為x
,最終輸出結果為Hellx, Wxrld!
。
丸趣 TV 網 – 提供最優質的資源集合!
正文完