The Java String class replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string.
The method replaceAll() replaces all occurrences of a String in another String matched by regex.
This is similar to the replace() function, the only difference is, that in replaceAll() the String to be replaced is a regex while in replace() it is a String.
Signature:
public String replaceAll(String regex, String replacement)
Parameters
regex : regular expression
replacement : replacement sequence of characters
Returns
replaced string
Exception Throws
PatternSyntaxException: if the syntax of the regular expression is not valid.
Internal implementation
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex)
.matcher(this)
.replaceAll(replacement);
}
Java String replaceAll() example 1: replace character
Let's see an example to replace all the occurrences of a single character.
FileName: StringReplaceAll.java
public class StringReplaceAll {
public static void main(String args[]){
String name="ngeducate is a good website";
String replaceString=name.replaceAll("a","b");
//replaces all occurrences of "a" to "b"
System.out.println(replaceString);
}
}
Output:
ngeducbte is b good website
Java String replaceAll() example 2: replace word
Let's see an example to replace all the occurrences of a single word or set of words.
FileName: StringReplaceAll.java
public class StringReplaceAll {
public static void main(String args[]){
String s1="My name is nagi. " +
"My name is reddy. My name is gajjela.";
//replaces all occurrences of "is" to "was"
String replaceString=s1.replaceAll("is","was");
System.out.println(replaceString);
}
}
Output:
My name was nagi. My name was reddy. My name was gajjela.
Java String replaceAll() example 3: remove white spaces
Let's see an example to remove all the occurrences of white spaces.
FileName: StringReplaceAll.java
public class StringReplaceAll {
public static void main(String args[]){
String s1="My name is nagi. " +
"My name is reddy. My name is gajjela.";
String replaceString=s1.replaceAll("\\s","");
System.out.println(replaceString);
}
}
Output:
Mynameisnagi.Mynameisreddy.Mynameisgajjela.
Java String replaceAll() example 4: remove https or http or www or ftp if String contains url
Let's see an example to remove all the occurrences of https, http, www, ftp if string contains url.
FileName: StringReplaceAll.java
public class StringReplaceAll {
public static void main(String args[]){
StringReplaceAll stringReplaceAll = new StringReplaceAll();
String urlString = "https://blogger.com/blog/posts/4673535940712407984";
boolean value = stringReplaceAll.containsURL(urlString);
if(value){
System.out.println(urlString
.replaceAll("(http:\\/\\/|https:\\/\\/|ftp:\\/\\/|file:\\/\\/)?(www.)?", ""));
}
}
private boolean containsURL(String content){
String REGEX = "((http:\\/\\/|https:\\/\\/|ftp:\\/\\/|file:\\/\\/)?(www.)?(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_\\/\\.0-9#:?=&;,]*)?)?)";
Pattern p = Pattern.compile(REGEX,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(content);
return m.find();
}
}
Output:
blogger.com/blog/posts/4673535940712407984
Java replace all special characters in string
public class StringTest {
public static void main(String[] args) {
String str= "This#string%contains^special*characters&.";
str = str.replaceAll("[^a-zA-Z0-9]"," ");
System.out.println(str);
}
}
Output:-
This string contains special characters
0 Comments