用java中的另一个字符串替换字符串什么函数可以用另一个字符串替换一个字符串?示例1:什么将取代"HelloBrother"带着"Brother"?示例2:什么将取代"JAVAISBEST"带着"BEST"?
3 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
方法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);方法2Pattern.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);方法3Apache Commons
java.lang.String, java.lang.String)
添加回答
举报
0/150
提交
取消
