1

37 文字を超える文字列が存在する場合、文字列を複数の行に分割する方法は?

例文

怠惰な犬を飛び越えた速い茶色のキツネ

それはそれを作る必要があります


怠惰な犬を飛び越えた速い茶色のキツネ

37文字目は「L」ですが

単語ごとにグループ化したい。

これが私のコードです

private string sentence(string statement)
{
    string completedWord = "";
    string temp = "";
    string[] wordArray = statement.Split(' ');
    for (int i = 0; i < wordArray.Length; i++)
    {
        temp = completedWord + wordArray[i] + ' ';
        if (temp.Length < 37)
        {
            completedWord = completedWord + wordArray[i] + ' ';
        }
        else
        {
            completedWord = completedWord + "\n" + wordArray[i] + ' ';
        }
        temp = "";
    }
    return completedWord;
}

文章が 37 文字になると、それは続きelseます。を追加する前に、各行を 37 にしたい\n。これは、文が 37 文字をはるかに超える場合にのみ発生します

4

8 に答える 8

3

これでうまくいくはずです。ちなみに、便宜上 StringBuilder を使用します。

static string sentence(string statement)
{
  if (statement.Length > 37)
  {
    var words = statement.Split(' ');        
    StringBuilder completedWord = new StringBuilder();
    int charCount = 0;

    if (words.Length > 1)
    {
      for (int i = 1; i < words.Length - 1; i++)
      {
        charCount += words[i].Length;
        if (charCount >= 37)
        {
          completedWord.AppendLine();
          charCount = 0;
        }

        completedWord.Append(words[i]);
        completedWord.Append(" ");
      }
    }

    // add the last word
    if (completedWord.Length + words[words.Length - 1].Length >= 37)
    {
      completedWord.AppendLine();
    }
    completedWord.Append(words[words.Length - 1]);
    return completedWord.ToString();
  }
  return statement;
}
于 2013-04-03T17:11:14.503 に答える
2

私はこれを使用します:

    /// <summary>
    /// Wrap lines in strings longer than maxLen by interplating new line
    /// characters.
    /// </summary>
    /// <param name="lines">the lines to process</param>
    /// <param name="maxLen">the maximum length of each line</param>
    public static string[] wrap_lines(string[] lines, int maxLen)
    {
        List<string> output = new List<string>();

        foreach (var line in lines)
        {
            var words = line.Split(' ');
            string newWord = words[0] + " ";
            int len = newWord.Length;

            for (int i = 1; i < words.Length; i++)
            {
                if (len + words[i].Length + 1 > maxLen)
                {
                    len = 0;
                    newWord += "\n";
                    i--;
                }
                else
                {
                    len += words[i].Length + 1;
                    string ch = i == words.Length - 1 ? "" : " ";
                    newWord += words[i] + ch;
                }
            }
            output.Add(newWord);
        }
        return output.ToArray();
    }

より長い単語はないと仮定しmaxLenます。

于 2021-02-20T04:18:37.217 に答える
0

forループを修正しました:

for (int i = 0; i < wordArray.Length; i++)
            {
                //temp = completedWord + wordArray[i] + ' ';      //remove it
                temp = temp + wordArray[i] + ' ';    //added
                if (temp.Length < 37)
                {
                    completedWord = completedWord + wordArray[i] + ' ';
                }
                else
                {
                    completedWord = completedWord + "\n";    //corrected
                    temp = "";     //added
                }
                //temp = "";       //remove it
            }
于 2013-04-03T17:06:10.020 に答える
0

現在記録されている行数を記録するフィールドを含めることができます。

string completedWord = "";
    string temp = "";
    string[] wordArray = statement.Split(' ');
    int lines = 1;
    for (int i = 0; i < wordArray.Length; i++)
    {

        temp = completedWord + wordArray[i] + ' ';
        if (temp.Length < 37* lines)
        {
            completedWord = completedWord + wordArray[i] + ' ';
        }
        else
        {
            completedWord = completedWord + "\n" + wordArray[i] + ' ';
            lines += 1;
        }
        temp = "";
    }
于 2013-04-03T17:07:39.057 に答える