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

时间:2020-12-31 07:10:10

What function can replace a string with another string?

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

Example #1: What will replace "HelloBrother" with "Brother"?

示例1:什么将用“Brother”取代“HelloBrother”?

Example #2: What will replace "JAVAISBEST" with "BEST"?

示例2:什么将用“BEST”取代“JAVAISBEST”?

5 个解决方案

#1


118  

The replace method is what you're looking for.

替换方法就是你要找的。

For example:

例如:

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

#2


34  

Try this: http://download.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28char,%20char%29

试试这个:http://download.oracle.com/javase/7/docs/api/java/lang/String.html取代% 28 char,% 20字符% 29

String a = "HelloBrother How are you!";
String r = a.replace("HelloBrother","Brother");

System.out.println(r);

This would print out "Brother How are you!"

这会打印出“兄弟你好!”

#3


8  

There is a possibility not to use extra variables

有可能不使用额外的变量

String s = "HelloSuresh";
s = s.replace("Hello","");
System.out.println(s);

#4


6  

Replacing one string with another can be done in the below methods

用另一个字符串替换一个字符串可以在下面的方法中完成

Method 1: Using String replaceAll

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

Method 2: Using Pattern.compile

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

Method 3: Using Apache Commons as defined in the link below:

方法3:使用下面链接中定义的Apache Commons:

http://commons.apache.org/proper/commons-lang/javadocs/api-z.1/org/apache/commons/lang3/StringUtils.html#replace(java.lang.String, java.lang.String, java.lang.String)

REFERENCE

参考

#5


5  

     String s1 = "HelloSuresh";
     String m = s1.replace("Hello","");
     System.out.println(m);

#1


118  

The replace method is what you're looking for.

替换方法就是你要找的。

For example:

例如:

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

#2


34  

Try this: http://download.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28char,%20char%29

试试这个:http://download.oracle.com/javase/7/docs/api/java/lang/String.html取代% 28 char,% 20字符% 29

String a = "HelloBrother How are you!";
String r = a.replace("HelloBrother","Brother");

System.out.println(r);

This would print out "Brother How are you!"

这会打印出“兄弟你好!”

#3


8  

There is a possibility not to use extra variables

有可能不使用额外的变量

String s = "HelloSuresh";
s = s.replace("Hello","");
System.out.println(s);

#4


6  

Replacing one string with another can be done in the below methods

用另一个字符串替换一个字符串可以在下面的方法中完成

Method 1: Using String replaceAll

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

Method 2: Using Pattern.compile

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

Method 3: Using Apache Commons as defined in the link below:

方法3:使用下面链接中定义的Apache Commons:

http://commons.apache.org/proper/commons-lang/javadocs/api-z.1/org/apache/commons/lang3/StringUtils.html#replace(java.lang.String, java.lang.String, java.lang.String)

REFERENCE

参考

#5


5  

     String s1 = "HelloSuresh";
     String m = s1.replace("Hello","");
     System.out.println(m);