I have a java string with " "
from a text file the program accesses with a Buffered Reader object. I have tried string.replaceAll(" ","")
and it doesn't seem to work.
Any ideas?
cleaned = cleaned.replace(" "," ");
I have a java string with " "
from a text file the program accesses with a Buffered Reader object. I have tried string.replaceAll(" ","")
and it doesn't seem to work.
Any ideas?
cleaned = cleaned.replace(" "," ");
The same way you mentioned:
String cleaned = s.replace(" "," ");
It works for me.
Strings are immutable so You need to do
string = string.replaceAll(" ","")
String.replace(char, char)
takes char
inputs (or CharSequence
inputs)
String.replaceAll(String, String)
takes String
inputs
For example:
String origStr = "bat";
String newStr = str.replace.('a', 'i');
// Now:
// origStr = "bat"
// newStr = "bit"
The key point is that the return value contains the new edited String
. The original String
variable that invokes replace()
/replaceAll()
doesn't have its contents changed.
For example:
String origStr = "how are you?";
String newStr = origStr.replaceAll(" "," ");
String anotherStr = origStr.replaceAll(" ","");
// origStr = "how are you?"
// newStr = "how are you?"
// anotherStr = howareyou?"
the string in java are immutable... you have to do
String newStr = cleaned.replaceAll(" ", "");
This works correclty.
Why? It must be XML or HTML, i.e. a markup language, so why are you removing the author's markup?