Java String Replace : Replaces the matched words in a String - Coded Java

Latest

Quality & Simple Java Content

Jun 2, 2018

Java String Replace : Replaces the matched words in a String

Java String Replace

Replaces the matched words in a String

replace()

"Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar"

System.out.println(" D ev  Dum my ".replace(" ",""));

"DevDummy"

replaceAll()

"Replaces each substring of this string that matches the given regular expression with the given replacement"

System.out.println(" D ev  Dum my ".replaceAll(" ",""));
System.out.println(" D ev  Dum my ".replaceAll("\\s+",""));

"DevDummy"
"DevDummy"

Note: "\\s+" is the regular expression similar to the empty space character.

Reference 

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

...

No comments:

Post a Comment