In spliting a string I want to generate a sequence of token-delimiter pairs. Thus, with ,
and ;
as my delimiters, I want " a , b;"
to produce new int[][]{{" a ",","},{" b",";"},{"",""}}
. The last entry indicates that the string ends with a delimiter. Of course, two consecutive delimiters are to be separated by and empty token.
Asked
Active
Viewed 135 times
-4

user2864740
- 60,010
- 15
- 145
- 220

jalexbnbl
- 15
- 1
- 1
-
3I'm sorry, but it is not clear what you are asking – Alan Aug 31 '14 at 02:36
-
2int[][]??? I see strings there... – Claudio Redi Aug 31 '14 at 02:39
-
1I see "I want" (Twice!) but I don't see code or a specific question. – HABO Aug 31 '14 at 02:39
1 Answers
1
Neither String.Split
nor Regex.Split
allow such an association - the result is always a sequence of strings. Even when also capturing the split token in the sequence as so the delimiter will be mixed-in.
However this task can be accomplished easily with Regex.Matches (or Match/NextMatch). The trick is to use the \G
anchor (see Anchors in Regular Expressions) such that the matching is incremental and resumes from the previous match.
var input = @" a , b;whatever";
// The \G anchor ensures the next match begins where the last ended.
// Then non-greedily (as in don't eat the separators) try to find a value.
// Finally match a separator.
var matches = Regex.Matches(input, @"\G(.*?)([,;])")
.OfType<Match>();
// All the matches, deal with pairs as appropriate - here I simply group
// them into strings, but build a List of Pairs or whatnot.
var res = matches
.Select(m => "{" + m.Groups[1].Value + "|" + m.Groups[2].Value + "}");
// res -> Enumerable with "{ a |,}", "{ b|;}"
String trailing;
var lastMatch = matches.LastOrDefault();
if (lastMatch != null) {
trailing = input.Substring(lastMatch.Index + lastMatch.Length);
// If the separator was at the end, trailing is an empty string
} else {
// No matches, the entire input is trailing.
trailing = input;
}
// trailing -> "whatever"
Have fun filling in the details (and fixing any issues) as required. For tidiness, modify this code as appropriate and put it inside a method.

Community
- 1
- 1

user2864740
- 60,010
- 15
- 145
- 220
-
I see I did not ask a direct question. The question is, "does string.split provide a means for retrieving delimiters from a delimited string." I am hoping that I might be able to avoid indexOf and regex. – jalexbnbl Aug 31 '14 at 03:56