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

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -