How to split a string to List<string> without splitting words?
Let's say we have a long string we wish to split into strings 64 chars long without splitting individual words:
We prefer questions that can be answered, not just discussed. If your question is about this website, ask it on meta instead.
If we split as such:
string.SplitByLength(64).ToList();
We'll end up with two strings:
We prefer questions that can be answered, not just discussed. I
f your question is about this website, ask it on meta instead.
What is the most elegant way of splitting this string so that the first string ends before the If
and the seconds string begins with If
?
In other words, how to split a long string into a list of strings equ开发者_开发知识库al to or shorter than the desired length while not splitting any of the individual words but rather splitting at the last possible empty space in between words?
You can give it a max value like 64, and then use that as a index to search backwards and find the first space and split it there. Repeat on the remaining string using recursion and you're done.
public static IEnumerable<string> SmartSplit(this string input, int maxLength)
{
int i = 0;
while(i + maxLength < input.Length)
{
int index = input.LastIndexOf(' ', i + maxLength);
if(index<=0) //if word length > maxLength.
{
index=maxLength;
}
yield return input.Substring(i, index - i);
i = index + 1;
}
yield return input.Substring(i);
}
var phrase = "We prefer questions that can be answered, not just discussed. If your question is about this website, ask it on meta instead. We prefer questions that can be answered, not just discussed. If your question is about this website, ask it on meta instead.";
var regex = new Regex(@"(.{1,64})(?:\s|$)");
var results = regex.Matches(phrase)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
EDIT: I got it...
I am not quite sure whether it is the most elegant, but why don't you split string on spaces and then have one integer as counter and if it is below 64 you add word and space to stringbuilder, and if it is above 64 then you add current stringbuilder to list, create new stringbuilder and reset counter?
精彩评论