No matter what I seem to try, replacing the white space with an input of
2x= -3
will result in the string being truncated to just 2x=.
public void parseEquation(String x){
String adf = x;
String z = adf.replaceAll("\\s","");
System.out.println(z);
}
The first line is my input, the next line is my output
fail
2 Answers 2
Turns out i have severe mental problems.
I was using the
new Scanner.next();
vs
new Scanner.nextLine();
1 Comment
If using other libraries is an option, consider StringUtils.deleteWhitespace . Its a well known library, well tested, and used in lots of projects so it is likely better and faster than what you could do on your own.
It also takes care of tabs and other non-printables. I'm not sure if the regex \s does that or not.
StringBuilderfor such string operations