I can use this:
String str = "TextX Xto modifyX"; str = str.replace('X','');//that does not work because there is no such character ''
Is there a way to remove all occurrences of character X
from a String in Java?
I tried this and is not what I want: str.replace('X',' '); //replace with space
Approach: The idea is to use erase() method and remove() function from C++ STL. Below is the syntax to remove all the occurrences of a character from a string. Below is the implementation of the above approach: C++
Remove all instances of a character from string using filter() and join() In Python, you can use the filter() function to filter all the occurrences of a characters from a string.
Remove All Occurrences of a Character From a String in Python Using the filter() Function. We can also use the filter() function with the join() method and lambda function to remove the occurrences of a character from a string in Python.
Try using the overload that takes CharSequence
arguments (eg, String
) rather than char
:
str = str.replace("X", "");
Using
public String replaceAll(String regex, String replacement)
will work.
Usage would be str.replace("X", "");
.
Executing
"Xlakjsdf Xxx".replaceAll("X", "");
returns:
lakjsdf xx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With