LINQ - 拆分一个字符串,最大长度,但不砍字拆开 [英] LINQ - Splitting up a string with maximum length, but not chopping words apart
问题描述
我有一个简单的LINQ扩展方法...
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).
不过我吸在LINQ,所以我想知道是否有人对如何去这样的想法。我知道我想要做什么,但我不知道如何处理它。
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.
我把这种方法 SplitOnLength(6)
我会得到下面的。
I call this method SplitOnLength(6)
I would get the following.
- 这是我
- 莎莎
- mple b
- 锁定0
- ˚F文本
- 我
- 将
- 通过牛逼
- hrough
- 在S
- 特林
- splitt
- 呃。
- This i
- s a sa
- mple b
- lock o
- f text
- that I
- would
- pass t
- hrough
- the s
- tring
- splitt
- er.
我宁愿它是足够聪明,停下来看更像..
I would rather it be smart enough to stop and look more like ..
- 这
- 是
- 样品
//坏榜样,因为一个单词超过了最大长度,但长度会在现实情况下更大的数字,接近200。
// bad example, since the single word exceeds maximum length, but the length would be larger numbers in real scenarios, closer to 200.
谁能帮助我
推荐答案
我将通过循环解决这个问题:
I'll solve this by for loop:
var ret1 = str.Split(' ');
var ret2 = new List<string>();
ret2.Add("");
int index = 0;
foreach (var item in ret1)
{
if (item.Length + 1 + ret2[index].Length <= allowedLength)
{
ret2[index] += ' ' + item;
if (ret2[index].Length >= allowedLength)
{
ret2.Add("");
index++;
}
}
else
{
ret2.Add(item);
index++;
}
}
return ret2;
首先我想过一个邮编,但在这里并不好。
first I thought about a Zip, but it's not good here.
和递延执行版本与产量:
and differed execution version with yield:
public static IEnumerable<string> SaeedsApproach(this string str, int allowedLength)
{
var ret1 = str.Split(' ');
string current = "";
foreach (var item in ret1)
{
if (item.Length + 1 + current.Length <= allowedLength)
{
current += ' ' + item;
if (current.Length >= allowedLength)
{
yield return current;
current = "";
}
}
else
{
yield return current;
current = "";
}
}
}
这篇关于LINQ - 拆分一个字符串,最大长度,但不砍字拆开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!