为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村225694 2019-07-03 10:19:58
用java中的另一个字符串替换字符串什么函数可以用另一个字符串替换一个字符串?示例1:什么将取代"HelloBrother"带着"Brother"?示例2:什么将取代"JAVAISBEST"带着"BEST"?
查看完整描述

3 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

这个replace方法就是你要找的东西。

例如:

String replacedString = someString.replace("HelloBrother", "Brother");


查看完整回答
反对 回复 2019-07-03
?
慕丝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);

方法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)

参照系


查看完整回答
反对 回复 2019-07-03
  • 3 回答
  • 0 关注
  • 2953 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号