replace String with another in java

JavaString

Java Problem Overview


What function can replace a string with another string?

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

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

Java Solutions


Solution 1 - Java

The replace method is what you're looking for.

For example:

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

Solution 2 - Java

Try this: https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%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!"

Solution 3 - Java

There is a possibility not to use extra variables

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

Solution 4 - Java

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

Method 1: Using String 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

 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:

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][1] [1]:<https://stackoverflow.com/a/25258335/3492139>

Solution 5 - Java

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

Solution 6 - Java

Another suggestion, Let's say you have two same words in the String

String s1 = "who is my brother, who is your brother"; // I don't mind the meaning of the sentence.

replace function will change every string is given in the first parameter to the second parameter

System.out.println(s1.replace("brother", "sister")); // who is my sister, who is your sister

and you can use also replaceAll method for the same result

System.out.println(s1.replace("brother", "sister")); // who is my sister, who is your sister

if you want to change just the first string which is positioned earlier,

System.out.println(s1.replaceFirst("brother", "sister")); // whos is my sister, who is your brother.

Solution 7 - Java

You can use replace method to achieve this:

String outputString1 = inputString.replace("HelloBrother", "Brother");
String outputString2 = inputString.replace("JAVAISBEST", "BEST");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShahView Question on Stackoverflow
Solution 1 - JavapwcView Answer on Stackoverflow
Solution 2 - JavaProNeticasView Answer on Stackoverflow
Solution 3 - JavaOleg SHView Answer on Stackoverflow
Solution 4 - JavaNishanthi GrashiaView Answer on Stackoverflow
Solution 5 - JavaDead ProgrammerView Answer on Stackoverflow
Solution 6 - JavaUser8500049View Answer on Stackoverflow
Solution 7 - JavaAman KumayuView Answer on Stackoverflow