c# word wrap,如何限制基于字符串中不同单词的新​​行创建

c# word wrap, how to limit new line creation based on different words in a string

我一直在考虑用 C# 编写自动换行控制台应用程序。我在这里找到了很多有用的例子,但我对目前的例子有疑问。

我目前得到的代码会在 "overflows" columnLimit 的每个单词后创建一个新行。我 want/need 一种仅当该单词不是字符串中的最后一个时才添加新行的方法,并且如果可能的话,当单词 overflows 在两行之间时添加连字符(所以它将是分词器而不是分词器)。

我知道这可能是一个非常简单的解决方案,但我是 C# 的新手,我真的被这个问题困住了。一个编码解决方案,这样我就可以看到我哪里出错了。

谁能帮我解决这个问题?

public string formatText(string longSentence, int columnLimit)
{
    string[] words = longSentence.Split(' ');
    StringBuilder newLongSentence = new StringBuilder();
    string line = "";

    foreach (string word in words)
    {
        if ((line + word).Length > columnLimit)
        {
            newLongSentence.AppendLine(line);
            line = "";
        }

        line += string.Format("{0} ", word);
    }

    if (line.Length > 0) 
        newLongSentence.AppendLine(line);

    return newLongSentence.ToString();     
}

试试这个

  public string formatText(string textData)
  { 
            textData = new Regex(@" {2,}").Replace(textData.Trim(), @" ");
            var left = Console.CursorLeft; var top = Console.CursorTop; var lines = new List<string>();
            for (var i = 0; textData.Length > 0; i++)
            {
                lines.Add(textData.Substring(0, Math.Min(Console.WindowWidth, textData.Length)));
                var length = lines[i].LastIndexOf(" ", StringComparison.Ordinal);
                if (length > 0) lines[i] = lines[i].Remove(length);
                textData = textData.Substring(Math.Min(lines[i].Length + 1, textData.Length));
                Console.SetCursorPosition(left, top + i); Console.WriteLine(lines[i]);
            }
  }

如果 columnLimit 很重要,那么以下内容可能会对您有所帮助

public string formatText(string longSentenceParts,int columnLimit)
{
        string[] words = longSentence.Split(new char[] { ' ' });
        IList<string> longSentenceParts = new List<string>();
        longSentenceParts.Add(string.Empty);

        int partCounter = 0;

        foreach (string word in words)
        {
            if ((longSentenceParts[partCounter] + word).Length > columnLimit)
            {
                partCounter++;
                longSentenceParts.Add(string.Empty);
            }

            longSentenceParts[partCounter] += word + " ";
        }

        foreach (string x in longSentenceParts)
            Console.WriteLine(x);
}

你应该使用行超过限制的长度来做一个子字符串,让你的 foreach 循环看起来更像这样

foreach (string word in words)
{
    int extraLetters = columnLimit - (line + word).Length;
    if (extraLetters < 0 && extraLetters != word.Length)
    {

        newLongSentence.AppendLine(line + word.Substring(0, word.length + extraLetters));
        line = string.Format("{0} ", word.Substring(word.length + extraLetters + 1));

    }
    else if(extraLetters == word.Length)
    {
        newLongSentence.AppendLine(line);
        line = String.Format("{0} ", word);
    }
    else
    {
        line += string.Format("{0} ", word);
    }

}

如果你想包含破折号,从相关的子字符串中减去 1 并像这样添加一个破折号

foreach (string word in words)
{
    int extraLetters = columnLimit - (line + word).Length;
    if (extraLetters < 0 && extraLetters != word.Length && extraLetters != word.Length - 1)
    {

        newLongSentence.AppendLine(line + word.Substring(0, word.length + extraLetters - 1) + "-");
        line = string.Format("{0} ", word.Substring(word.length + extraLetters));

    }
    else if(extraLetters == word.Length || extraLetters == word.Length - 1)
    {
        newLongSentence.AppendLine(line);
        line = String.Format("{0} ", word);
    }
    else
    {
        line += string.Format("{0} ", word);
    }

}