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
If you want to do something with Java Strings, Commons Lang StringUtils is a great place to look.
Using
will work.
Usage would be
str.replace("X", "");
.Executing
returns:
Use replaceAll instead of replace
This should give you the desired answer.
I like using RegEx in this occasion:
where g means global so it will go through your whole string and replace all X with ''; if you want to replace both X and x, you simply say:
(see my fiddle here: fiddle)
You can use
str = str.replace("X", "");
as mentioned before and you will be fine. For your information''
is not an empty (or a valid) character but'\0'
is.So you could use
str = str.replace('X', '\0');
instead.