I have a simple LINQ Extension Method...
public static IEnumerable<string> SplitOnLength(this string input, int length)
{
int index = 0;
while (index < input.Length)
{
if (index + length < input.Length)
yield return input.Substring(index, length);
else
yield return input.Substring(index);
index += length;
}
}
This takes a string, and it chops it up into a collection of strings that do not exceed the given length.
This works well - however I'd like to go further. It chops words in half. I don't need it to understand anything complicated, I just want it to be able to chop a string off 'early' if cutting it at the length
would be cutting in the middle of text (basically anything that isn't whitespace).
However I suck at LINQ, so I was wondering if anyone had an idea on how to go about this. I know what I am trying to do, but I'm not sure how to approach it.
So let's say I have the following text.
This is a sample block of text that I would pass through the string splitter.
I call this method SplitOnLength(6)
Я бы получил следующее.
Я бы предпочел, чтобы он был достаточно умен, чтобы остановиться и больше походить на ..
// плохой пример, так как одно слово превышает максимальную длину, но в реальных сценариях длина будет больше, чем 200.
Кто-нибудь может мне помочь?