简体   繁体   中英

C# StringBuilder - Wrapping Text to Another Line

THE PROBLEM

The problem I am running into is trying to get text to wrap to another line properly through a function. What I would like it to do is to wrap like it would in a word editor to the next line cleanly.

THE CODE

The code for the function and all other relevant information required:

// 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);
            }
        }

THE RESULTS

What I expect the text to look like after inputting it is:

This is a really long string intended to demonstrate how words should wrap to the next line of the console. If I extending the string longer, it should wrap.

What it actually looks like:

You can use the following: to record a line in your view, example:

var new text = "text"

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

If you want to change or collate something in this text you can use it that way.

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

If the item you want to change is a list of objects you can use:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM