簡體   English   中英

拆分字符串,但最后返回整個單詞

[英]Split String but Return whole word at the end

我正在使用以下功能將字符串拆分成塊

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如何,最后一個單詞都是完整單詞

您可以將字符串拆分為單詞,然后嘗試生成大小至少為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;
}

您可以做這樣的事情,但這有點難看,因為它在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;
});

也試試這個:

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?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM