How to split a string, but also keep the delimiters?
Use Java's Pattern
class with a regex containing positive lookahead (?=regex)
and positive lookbehind (?<=regex)
assertions. Use "(?<=delimiter)|(?=delimiter)"
as your pattern. Check out this elegant code snippet:
The focus here is commas as delimiters. Swap ", " with your particular delimiter to fetch the expected outcome.
Handling dynamic delimiters: String.format in action!
Working with various delimiters in the mix? String.format
comes to the rescue, enabling injection of specific delimiters into your regex patterns:
Calling Pattern.quote(delimiter)
ensures that your delimiter is treated as a literal string, dodging conflicts with regex special characters.
Enhancing readability in complex regex
Keeping your regex readable promotes maintainability. Consider this:
Defining complexDelimiter
holds several delimiters separated by the pipe character |
, improving the code's readability.
Pattern and Matcher: Your allies in string manipulation
For those times when String.split()
is just too mainstream, meet the Pattern
and Matcher
classes:
Considering edge cases
Beware of empty elements at the beginning or end of your result array. Filters to the rescue:
StringTokenizer: Keeping the tokens together
Say hello to the StringTokenizer
class, sparing no delimiter:
Setting true
instructs StringTokenizer
to return delimiters as tokens.
Customizability with Guava's Splitter
Sometimes, a little vanity doesn't hurt. Customize your splitting with Guava's Splitter:
Simplifying complexity: Back-to-basic with replace
Sometimes keeping it simple is the best approach:
Pump up the uniqueness - use unique characters as placeholders for safe splitting and keeping those precious delimiters intact.
Was this article helpful?