Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a string in Java and retain the delimiters?

Tags:

java

I have this string (Java 1.5):

:alpha;beta:gamma;delta

I need to get an array:

{":alpha", ";beta", ":gamma", ";delta"}

What is the most convenient way to do it in Java?

like image 391
yegor256 Avatar asked Sep 23 '10 10:09

yegor256


People also ask

How do you split a string but keep the delimiters?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9] .

How can you split a character having the combination of string special characters and numbers in Java?

String myString = "Jane-Doe"; String[] splitString = myString. split("-"); We can simply use a character/substring instead of an actual regular expression. Of course, there are certain special characters in regex which we need to keep in mind, and escape them in case we want their literal value.


2 Answers

str.split("(?=[:;])")

This will give you the desired array, only with an empty first item. And:

str.split("(?=\\b[:;])")

This will give the array without the empty first item.

  • The key here is the (?=X) which is a zero-width positive lookahead (non-capturing construct) (see regex pattern docs).
  • [:;] means "either ; or :"
  • \b is word-boundary - it's there in order not to consider the first : as delimiter (since it is the beginning of the sequence)
like image 79
Bozho Avatar answered Sep 17 '22 18:09

Bozho


To keep the separators, you can use a StringTokenizer:

new StringTokenizer(":alpha;beta:gamma;delta", ":;", true)

That would yield the separators as tokens.

To have them as part of your tokens, you could use String#split with lookahead.

like image 33
Fabian Steeg Avatar answered Sep 17 '22 18:09

Fabian Steeg