C# split string but keep separators -
there exist similar questions, of them use regexen. code i'm using (that strips separators):
string[] sentences = s.split(new string[] { ". ", "? ", "! ", "... " }, stringsplitoptions.none); i split block of text on sentence breaks , keep sentence terminators. i'd avoid using regexen performance. possible?
i don't believe there existing function this. can use following extension method.
public static ienumerable<string> splitandkeepseparators(this string source, string[] separators) { var builder = new text.stringbuilder(); foreach (var cur in source) { builder.append(cur); if (separators.contains(cur)) { yield return builder.tostring(); builder.length = 0; } } if (builder.length > 0) { yield return builder.tostring(); } }
Comments
Post a Comment