java 程式語言提供了多種字串操作函數,讓開發人員執行連線、格式化、分割等操作,並替換。字串替換的一個常用函數是 Replace() 方法。
replace() 方法接受兩個參數:
replace() 方法傳回一個新字串,其中所有出現的原始字串都被替換字串取代。
範例1: 將「HelloBrother」替換為「兄弟」
String originalString = "HelloBrother"; String replacementString = "Brother"; String replacedString = originalString.replace("HelloBrother", replacementString); System.out.println(replacedString); // Output: Brother
範例2:
String originalString = "JAVAISBEST"; String replacementString = "BEST"; String replacedString = originalString.replace("JAVAISBEST", replacementString); System.out.println(replacedString); // Output: BEST
以上是Java 的「replace()」方法如何進行字串運算?的詳細內容。更多資訊請關注PHP中文網其他相關文章!