拆分字符串但最后返回整个单词

时间:2018-04-29 14:02:05

标签: c#

我正在使用以下函数将字符串拆分为块

public static IList<string> SplitChunks(string text, int chunkSize)
{
    List<string> chunks = new List<string>();
    int offset = 0;
    while (offset < text.Length)
    {
        int size = Math.Min(chunkSize, text.Length - offset);
        chunks.Add(text.Substring(offset, size));
        offset += size;
    }
    return chunks;
}

工作正常,但问题是在许多情况下,块结尾有一个不完整的单词,如

输入:

  

字符串:大家好。你好吗?

     

尺寸:10

输出

  

你好吗

我希望它返回完整的最后一个单词,例如Hello Everyone

如何修改我的功能,以便最后一个单词是一个完整的单词,而不管块的size

3 个答案:

答案 0 :(得分:2)

您可以将字符串拆分为单词,然后尝试生成至少chunkSize大小的块:

public static IList<string> SplitChunks(string text, int chunkSize)
{
    var words = text.Split(' ');
    var result = new List<string>();
    int length = 0;
    string current = "";
    foreach(var word in words)
    {
        current += word + " ";
        length += word.Length + 1;

        if (length > chunkSize) {
            result.Add(current);
            current = "";
            length = 0;
        }
    }
    if (current != "")
        result.Add(current);
    return result;
}

答案 1 :(得分:1)

你可以这样做,但它有点难看,因为它在TakeWhile产生副作用:

int count = 0;
const string text = "Hello Everyone. How are you?";
var ret = text.TakeWhile(s =>
{
    var keepTaking = count < max;
    count += s.Length + 1; // +1 for the space between words
    return keepTaking;
});

答案 2 :(得分:0)

尝试这个:

public static IList<string> SplitChunks(string text, int chunkSize)
{
    var parts = text.Split(' ');
    return parts.Skip(1).Aggregate(parts.Take(1).ToList(), (a, x) =>
    {
        if ((a.Last() + x).Length > chunkSize)
            a.Add(x);
        else
            a[a.Count - 1] += " " + x;
        return a;
    });
}

当我致电SplitChunks("Hello Everyone. How are you?", 10)时,我明白了:

Hello 
Everyone. 
How are 
you?