Problem: spending too much time solving simple problems. Oh, here's the simple problem.
- Input:
string inStr,char delimiter - Output:
string[] outStrswherestring.Join("", outStrs) == inStrand each item inoutStrsbefore the last item must end with the delimiter. IfinStrends with the delimiter, then the last item inoutStrsends 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);
}
string.Split()method, the functionality just needs to be slightly different.foreach(string token in outStrs) { string value = token + delimiter; }?