Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace String with another in java

Tags:

java

string

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"?

like image 529
Shah Avatar asked Mar 07 '11 05:03

Shah


People also ask

How do I replace a string with another string in Java?

To replace one string with another string using Java Regular Expressions, we need to use the replaceAll() method. The replaceAll() method returns a String replacing all the character sequence matching the regular expression and String after replacement.

How do you replace a part of a string with another string?

The replace() method returns a copy of the string where the old substring is replaced with the new substring. The original string is unchanged. If the old substring is not found, it returns the copy of the original string.

How do you replace a string with another string in Java without replace method?

To replace a character in a String, without using the replace() method, try the below logic. Let's say the following is our string. int pos = 7; char rep = 'p'; String res = str. substring(0, pos) + rep + str.

What is replaceAll \\ s in Java?

Java String replaceAll() The Java String class replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string.


1 Answers

The replace method is what you're looking for.

For example:

String replacedString = someString.replace("HelloBrother", "Brother"); 
like image 143
pwc Avatar answered Oct 01 '22 14:10

pwc