2

I want to split string by setting all non-alphabet as separator.

String[] word_list = line.split("[^a-zA-Z]");

But with the following input

11:11 Hello World

word_list contains many empty string before "hello" and "world"

Please kindly tell me why. Thank You.

1
  • Do you expect as result with 11:11 Hello World as input? Commented Mar 17, 2012 at 6:13

3 Answers 3

2

Because your regular expression matches each individual non-alpha character. It would be like separating

",,,,,,Hello,World"

on commas.

You will want an expression that matches an entire sequence of non-alpha characters at once such as:

line.split("[^a-zA-Z][^a-zA-Z]*")

I still think you will get one leading empty string with your example since it would be like separating ",Hello,World" if comma were your separator.

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

2 Comments

Thanks blackcompe. I thought it would, but wasn't sure since I've used some regex implementations support + and others that don't. Wasn't sure about Java's String.split().
Finally, I first replace the heading non-alphabet character with empty string and then call split() with this regex :)
2

Here's your string, where each ^ character shows a match for [^a-zA-Z]:

11:11 Hello World
^^^^^^     ^

The split method finds each of these matches, and basically returns all substrings between the ^ characters. Since there's six matches before any useful data, you end up with 5 empty substrings before you get the string "Hello".

To prevent this, you can manually filter the result to ignore any empty strings.

1 Comment

I like ur explanation as it is very clear. But I can only give one tick,sorry.
0

Will the following do?

String[] word_list = line.replaceAll("[^a-zA-Z ]","").replaceAll(" +", " ").trim().split("[^a-zA-Z]");

What I am doing here is removing all non-alphabet characters before doing the split and then replacing multiple spaces by a single space.

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.