猿问

用java中的另一个字符串替换字符串

用java中的另一个字符串替换字符串

什么函数可以用另一个字符串替换一个字符串?

示例1:什么将取代"HelloBrother"带着"Brother"?

示例2:什么将取代"JAVAISBEST"带着"BEST"?


慕村225694
浏览 2954回答 3
3回答

偶然的你

这个replace方法就是你要找的东西。例如:String replacedString = someString.replace("HelloBrother", "Brother");

慕丝7291255

可以在以下方法中将一个字符串替换为另一个字符串方法1:使用字符串replaceAll String myInput = "HelloBrother";  String myOutput = myInput.replaceAll("HelloBrother", "Brother"); // Replace hellobrother with brother  ---OR---  String myOutput = myInput.replaceAll("Hello", ""); // Replace hello with empty  System.out.println("My Output is : " +myOutput);方法2*使用Pattern.compile import java.util.regex.Pattern;  String myInput = "JAVAISBEST";  String myOutputWithRegEX = Pattern.compile("JAVAISBEST").matcher(myInput).replaceAll("BEST");  ---OR -----  String myOutputWithRegEX = Pattern.compile("JAVAIS").matcher(myInput).replaceAll("");  System.out.println("My Output is : " +myOutputWithRegEX);方法3*使用Apache Commons如以下链接所定义:   java.lang.String, java.lang.String)参照系
随时随地看视频慕课网APP

相关分类

Java
我要回答