Problem: spending too much time solving simple problems. Oh, here's the simple problem.
- Input:
string inStr
,char delimiter
- Output:
string[] outStrs
wherestring.Join("", outStrs) == inStr
and each item inoutStrs
before the last item must end with the delimiter. IfinStr
ends with the delimiter, then the last item inoutStrs
ends with the delimiter as well.
Example 1:
- Input:
"my,string,separated,by,commas"
,','
- Output:
["my,", "string,", "separated,", "by,", "commas"]
Example 2:
- Input:
"my,string,separated,by,commas,"
,','
- Output:
["my,", "string,", "separated,", "by,", "commas,"]
(notice trailing comma)
Solution with Regex: here
I want to avoid using Regex, simply because this requires only character comparison. It's algorithmically just as complex to do as what string.Split()
does. It bothers me that I cannot find a more succinct way to do what I want.
My bad solution, which doesn't work for me... it should be faster and more succinct.
var outStr = inStr.Split(new[]{delimiter},
StringSplitOptions.RemoveEmptyEntries)
.Select(x => x + delimiter).ToArray();
if (inStr.Last() != delimiter) {
var lastOutStr = outStr.Last();
outStr[outStr.Length-1] = lastOutStr.Substring(0, lastOutStr.Length-1);
}