5

I was wondering if it was possible to split a string on whitespace, and avoid all numbers, whitespace and operators such as + and -

This is what I have, but I believe it is incorrect

  String [] temp = expression.split("[ \\+ -][0-9] ");

Suppose I have an expression

x+y+3+5+z

I want to get rid of everything else and only put x, y, and z into the array

3
  • 1
    split a string on whitespace, and avoid all ... whitespace? Can you give a few examples of what you want to happen on a variety of inputs and what the output should be? Commented Dec 11, 2010 at 21:40
  • What I mean is, that whitespace should not be in the array that I split Commented Dec 11, 2010 at 21:43
  • +1 for providing the example. That makes your question about 920 times more clear. Commented Dec 11, 2010 at 21:49

2 Answers 2

2

I think you mean this:

String[] temp = expression.split("[\\s0-9+-]+");

This splits on whitespace, 0 to 9, + and -. Note that the characters appear in a single character class, not multiple separate character classes. Also the - doesn't need escaping here because it is at the end of the character class.

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

5 Comments

your solution is good, but it gives extra blank spots in the array. Try it with 2 + y. It gives an array size of 3 but only y is in there. The other two are empty.
@Steffan Harris: That's how split works. Read the documentation: download.oracle.com/javase/1.5.0/docs/api/java/lang/… Trailing empty strings are therefore not included in the resulting array. Notice that it doesn't say that leading empty strings are removed. You'll probably have to remove them yourself, or alternatively don't use split, use a Matcher instead and write a regular expression for what you want to keep instead of what you want to discard.
yes, my solution has the same "problem" - a leading "separator" is treated as if there's any empty token in front of it.
Or you could do a expression.replaceAll(" ","").split("[\\s0-9+-]+"); No whitespace, no problem.
That doesn't work on Unicode, which just happens to be Java’s native character set.
1

I think this is what you're after:

String[] tmp = expression.split("[^a-zA-Z]+");

which should treat the separator as being anything that isn't a sequence of letters.

Test Run

public class foo {
    static public void main(String[] args) {
        String[] res = args[0].split("[^a-zA-Z]+");
        for (String r: res) {
            System.out.println(r);
        }
    }
}

% javac foo.java
% java foo x+y+3+5+z
x
y
z

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.