用java中的另一個(gè)字符串替換字符串什么函數(shù)可以用另一個(gè)字符串替換一個(gè)字符串?示例1:什么將取代"HelloBrother"帶著"Brother"?示例2:什么將取代"JAVAISBEST"帶著"BEST"?
3 回答

慕絲7291255
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
方法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)
添加回答
舉報(bào)
0/150
提交
取消