1

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
  • 2
    Use StringBuilder for such string operations
    – noMAD
    Commented Apr 26, 2013 at 16:31
  • String adf = x; is redundant here as replaceAll creates a new String
    – danieln
    Commented Apr 26, 2013 at 16:57

2 Answers 2

7

Turns out i have severe mental problems.

I was using the

new Scanner.next();

vs

new Scanner.nextLine();

1
  • 2
    severe mental problems
    – jsedano
    Commented Apr 26, 2013 at 16:33
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.