9

I essentially want to split up a string based on the sentences, therefore (for the sake of what I'm doing), whenever there is a !, ., ?, :, ;.

How would I achieve this with multiple items to split the array with?

Thanks!

4 Answers 4

28

String.split takes a regex to split on, so you can simply:

mystring.split("[!.?:;]");
Sign up to request clarification or add additional context in comments.

Comments

8

Guava's Splitter is a bit more predictable than String.split().

Iterable<String> results = Splitter.on(CharMatcher.anyOf("!.?:;"))
   .trimResults() // only if you need it
   .omitEmptyStrings() // only if you need it
   .split(string);

and then you can use Iterables.toArray or Lists.newArrayList to wrap the output results how you like.

Comments

5

The argument of String.split is a regex, so you can create a pattern that matches any of those characters.

s.split("[.!:;?]");

Comments

4

You can use the String.split(String regex) method with parameter "[!.?:;]".

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.