首页 > 解决方案 > C# StringBuilder - 将文本换行到另一行

问题描述

问题

我遇到的问题是试图通过一个函数让文本正确地换行到另一行。我想做的是像在文字编辑器中一样干净地换行到下一行。

编码

该函数的代码和所需的所有其他相关信息:

// Internal variables that store values.
// Should NOT be called upon.
        private int width;
        private int height;
        private int x;
        private int y;

// Getter/Setter for window Width.
        public int Width
        {
            get
            {
                width = Console.WindowWidth;
                return width;
            }
            private set
            {
                if (value <= 0)
                {
                    throw new Exception("Width setter: Invalid Width inputted");
                }
                else
                {
                    width = value;
                }
            }
        }

// Getter/Setter for window Height.
        public int Height
        {
            get
            {
                height = Console.WindowHeight;
                return height;
            }
            private set
            {
                if (value <= 0)
                {
                    throw new Exception("Height setter: Invalid height inputted");
                }
                else
                {
                    height = value;
                }
            }
        }

// Getter/Setter for cursor X position.
        public int X
        {
            get
            {
                x = Console.CursorLeft;
                return x;
            }
            private set
            {
                if ( value < 0 || value > Width )
                {
                    throw new Exception("X Setter: Invalid X position.");
                }
                else
                {
                    x = value;
                }
            }
        }

// Getter/Setter for cursor Y position.
        public int Y
        {
            get
            {
                y = Console.CursorTop;
                return y;
            }
            private set
            {
                if (value < 0 || value > Height)
                {
                    throw new Exception("Y Setter: Invalid Y position.");
                }
                else
                {
                    y = value;
                }
            }
        }
// SetCursorPosition is a method to, well, change the cursor position.
        public void SetCursorPosition(int newX, int newY)
        {
            this.X = newX;
            this.Y = newY;
            Console.SetCursorPosition(newX, newY);
        }

// WriteLine writes a line to the console.
// It also sanity checks the length of the string doesn't exceed the width
// of the window, and changes the string to be among two or even three lines
// if needed.
        public void WriteLine( int yPos, string String )
        {
            int stringLength = String.Length;
            if (stringLength > Width)
            {
                string[] textToSplit = String.Split();
                StringBuilder splitText = new StringBuilder();
                int currentLineLength = 0;
                for ( int i = 0; i < textToSplit.Length; i++ )
                {
                    if ( currentLineLength > Width )
                    {
                        if ( textToSplit[i].Length > Width - currentLineLength )
                        {
                            splitText.Append("\n");
                            splitText.Append(textToSplit[i]);
                            currentLineLength = 0;
                        }
                        else
                        {
                            splitText.Append(textToSplit[i]);
                            splitText.Append("\n");
                            currentLineLength = 0;
                        }

                    }
                    else
                    {
                        splitText.Append(textToSplit[i]);
                        splitText.Append(" ");
                        currentLineLength = currentLineLength + textToSplit[i].Length + 1;
                    }
                }
                Console.Write(splitText);

            }
            // The string fits on one line, so just print it out.
            else
            {
                SetCursorPosition(0, yPos);
                this.Y = yPos;
                Console.Write(String);
            }
        }

结果

我期望文本输入后的样子是:

这是一个非常长的字符串,旨在演示单词应该如何换行到控制台的下一行。如果我将字符串延长更长的时间,它应该换行。

它实际上是什么样子的:

标签: c#appendconsole-applicationstringbuilder

解决方案


您可以使用以下内容:在视图中记录一行,例如:

var new text = "text"

var cmd = @"what wish 
        show {change}";

如果您想更改或整理本文中的某些内容,您可以这样使用它。

cmd = cmd.Replace ("{change}", new text);

如果您要更改的项目是您可以使用的对象列表:

List <int> numbers = new List <int> (new int [] {2, 3, 5});

cmd = cmd.Replace ("{change}", string.Join (",", numbers));

推荐阅读