在空格处拆分长串

时间:2013-12-03 20:00:12

标签: c# .net string

在我的程序中,如果字符串太长,我需要将字符串拆分成多行。现在我正在使用这种方法:

private List<string> SliceString(string stringtocut)
{
    List<string> parts = new List<string>();
    int i = 0;
    do
    {  
        parts.Add(stringtocut.Substring(i, System.Math.Min(18, stringtocut.Substring(i).Length)));
        i += 18;
    } while (i < stringtocut.Length);
    return parts;
}

唯一的问题是,如果第19个角色不是空格,我们会将一个单词减少一半,看起来非常糟糕。

E.g。

字符串: 这是一封超过18个字母的长篇小说。

Sliced string: 
This is a long sent
ance with more than
 18 letters.

我如何剪切字符串,使其每个部分不超过18个字符,但如果可以的话,请回到最近的空格?我一直在玩弄上述算法,但我似乎无法得到它。

谢谢!

3 个答案:

答案 0 :(得分:15)

也许使用这样的正则表达式:

var input = "This is a long sentence with more than 18 letters.";
var output = Regex.Split(input, @"(.{1,18})(?:\s|$)")
                  .Where(x => x.Length > 0)
                  .ToList();

返回结果:

[ "This is a long", "sentence with more", "than 18 letters." ]

<强>更新

这是一个类似的解决方案,可以处理很长的单词(虽然我觉得它的表现不会很好,所以你可能想要对此进行基准测试):

var input = "This is a long sentence with a reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally long word in it.";
var output = Regex.Split(input, @"(.{1,18})(?:\s|$)|(.{18})")
                  .Where(x => x.Length > 0)
                  .ToList();

这会产生结果:

[ "This is a long", 
  "sentence with a", 
  "reallyreallyreally", 
  "reallyreallyreally", 
  "reallyreallyreally", 
  "reallyreallyreally", 
  "really long word", 
  "in it." ]

答案 1 :(得分:2)

这实际上并不是优化也不是优雅的代码,而是给出了理想的结果。重构应该相对容易:

string longSentence = "This is a long sentence with more than 18 letters.";

List<string> words = new List<string>();
string currentSentence = string.Empty;

var parts = longSentence.Split(' ');
foreach (var part in parts)
{
    if ((currentSentence + " " + part).Length < 18)
    {
        currentSentence += " " + part;
    }
    else
    {
        words.Add(currentSentence);
        currentSentence = part;
    }
}
words.Add(currentSentence);
words[0] = words[0].TrimStart();

结果:

This is a long
sentence with
more than 18
letters.

基本上,你要添加每个单词,直到你要打破18个字母。此时,您保存零件并重新开始。当它结束时,你加上剩下的东西。此外,在开始时需要一些不必要的空间需要修剪。

答案 2 :(得分:1)

试试这段代码:

int len = 0;
int index = 0;
text = string.Join(Environment.NewLine,
                   text.SplitBy(' ')
                       .GroupBy(w =>
                                { 
                                    if (len + w.Length > 18)
                                    {
                                        len = 0;
                                        index++;
                                    }
                                    len += w.Length + 1;
                                    return index;
                                })
                       .Select(line => string.Join(" ", line)));