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 Commented Apr 26, 2013 at 16:31
  • String adf = x; is redundant here as replaceAll creates a new String 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();

Sign up to request clarification or add additional context in comments.

1 Comment

severe mental problems
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.