Java 字符串 replace() 方法示例。
java 8object oriented programmingprogramming
String 类的 replace() 方法接受两个参数,即 −
- oldChar − 旧字符。
- newChar − 新字符。
返回一个新字符串,该字符串将所有出现的 oldChar 替换为 newChar。
示例
import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.replace('o', 'T')); System.out.print("Return Value :" ); System.out.println(Str.replace('l', 'D')); } }
输出
Return Value :WelcTme tT TutTrialspTint.cTm Return Value :WeDcome to TutoriaDspoint.com