-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Joining collector: possibility to specify custom delimiter used before the last stream element #21
Comments
A work around for this is...
The first group in the regular expression is greedy and will try to match the whole string. It will then have to backup until the ", " is found. Thus, the last ", " will be found and replaced with " or ". This regular expression assumes that ", " will not appear in any of the strings being joined. |
@nathanila, actually Joining collector is much more smart. For example, it can short-circuit based on maximal string length, cutting at character/codepoint/grapheme/word bound, etc. It's actually the hardest part here: how to properly combine this List<String> movies = asList("Snatch", "Revolver", "Locks, Stocks and Two Smoking Barrels");
System.out.println("Which movie do you want to see: "+
StreamEx.of(movies).map(m -> '"'+m+'"').collect(
Joining.with(", ").lastDelimiter(" or "))+"?"); I mean if the string contains the delimiter, the regexp will screw the things up. In general regexps should be used with caution: with unexpected input (unintentional or malicious) they may break your program. |
A custom delimiter could be specified to format more user-friendly messages like this:
Possible names for new method:
lastDelimiter
,beforeLast
. An open question is whether it should be used when short-circuit occurs.The text was updated successfully, but these errors were encountered: