Split string but keep separator in C#
Today I tried to split the string but preserve split characters. I didn’t find native methods for this need. If somebody know how to do it please write the comment.
Here is my code:
var separator = '@'; var result = new Dictionary<int, string>(); var key = 0; for (var i = 0; i < testString.Length; i++) { var c = testString[i]; if (c == separator) key++; if (result.ContainsKey(key)) { result[key] += c; } else { result.Add(key, c.ToString()); } } |