question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Splitter.onAny(String...)

See original GitHub issue

Sometimes, I’d like to split either on ';' or ','. For that, I can use a CharMatcher.ofAny(",;").

But it also happens that I want to split on "; " or ", " with spaces. Well, maybe those are parameters. So let’s just say I don’t know them at compile-time.

Good, I could write a regex. So let’s write it:

String[] separators = { "; ", ", " };
String splitterPattern = String.format("(?:%s|%s)", Pattern.quote(separators[0]), Pattern.quote(separators[1]));

Hmm… That’s verbose. And I have only two separators. Let’s make it generic for more :

String[] separators = { "; ", ", ", ". " };
String splitterPattern = "(?:" + FluentIterable.from(separators).transform(Pattern::quote).join("|") + ")";

Well, now that’s rational (yeah, I used FluentIterable and not a Stream because the Guava’s join is much, much better), but it’s not instinctive. It took me a good 30 minutes to get to something that small, pondering which of Stream or FluentIterable to use, and how to make it rather efficient.

So I’m suggesting to add another Splitter.on... method: Splitter.onAny(String...). Internally it could use regexes (that would make sense). Here’s my proposal:

public static Splitter onAny(String... separators) {
  return onPattern(
      Joiner.on("|").appendTo(new StringBuilder("(?:"), FluentIterable.from(separators).transform(Pattern::quote)).append(")").toString()
  );
}

Or if you still don’t like inter-package dependencies (as I once or twice read):

public static Splitter onAny(String... separators) {
  return onPattern(Arrays.stream(separators).map(Pattern::quote).collect(Collectors.joining("|", "(?:", ")"));
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ogregoirecommented, Aug 4, 2017

That’s an excellent idea! Actually, this can be the one killer feature for StringMatcher we were looking for (see discussion of #2692), as I’ve noted in that ticket that the feature is rather moot, if not directly inferior to regexes.

1reaction
jbduncancommented, Aug 3, 2017

A potential alternative, if deemed desirable, would be Splitter.on(StringMatcher.anyOf(String...)), where StringMatcher would come from issues https://github.com/google/guava/issues/2692 and https://github.com/google/guava/issues/1075.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A String splitter utility class which ignores delimiters between ...
A String splitter utility class which ignores delimiters between quotes. Has no external dependencies. - QuoteAwareStringSplitter.java.
Read more >
Archery String Splitter - Walmart.com
Archery String Splitter(1000+) · Compound Bow Archery Cable Slide Plastic String Splitter Separator Glide Kit · SPRING PARK Aluminum Compound Bow Cable Slide ......
Read more >
Java Splitter Examples: split, splitToList - Dot Net Perls
With Guava, a Java library, we use the Splitter class, and its "on" method for many advanced options. We can transform a String...
Read more >
Split a string by another string in C# - Stack Overflow
Is there a way to split a string , with another string being the split by parameter? I've tried converting the splitter into...
Read more >
How to split a string in C/C++, Python and Java?
Alternately, we can also utilise getline function to tokenize string on any single character delimiter. Some of the Most Common used functions ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found