隐藏长字符串中的文本以阻止创建新行

时间:2017-04-21 03:00:42

标签: c#

我想知道在C#中是否可以阻止控制台从长字符串中创建新行,而只是隐藏超出窗口宽度的文本。

例如,当我制作长字符串时,结果将是:

Blah blah blah, word stuff, word stuff, bl | <-- Window width
ah blah blah.                              | <-- Window width

我希望它是这样的:

Blah blah blah, word stuff, word stuff, bl | <-- Window width
                                           | <-- Window width

3 个答案:

答案 0 :(得分:1)

最好的方法是使用buffsize,如下所示:

int windowWidth = Console.WindowWidth;
string str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Console.Write(str.ToCharArray(), 0, windowWidth);

另一种方式是DIY。这样做有点困难,但您可以通过以下步骤完成: 1.获取当前窗口大小

int windowWidth = Console.WindowWidth;

2。获取当前字符串的显示长度,它与您的字体相关

Font font = new Font(FontFamily.GenericSansSerif, 11);
var size = TextRenderer.MeasureText(str, font).Width;

3。使用2个值来剪切字符串&amp;显示第1部分。

但这很复杂。

答案 1 :(得分:1)

您可以通过拦截Console标准输出来实现。

以下代码只是为了让您入门。如果你需要支持标签,随机光标定位,行结尾的混合(例如\ r \ n和\ n和\ r),那么它可能需要更多的工作。

ConsoleTruncator允许您定义“maxlinelength”,它控制每行上的最大字符数 - 如果控制台窗口宽度缩小到低于“maxlinelength”值,则不会停止发生包装。 / p>

相反,您可以使用类似的东西来“跟踪”控制台的宽度(非常接近):

bool bRoom = (Console.WindowWidth - 1 - m_columnposition) > 0;

对于简单的WriteWriteLines,我认为应该没问题。

更高级的方法是捕获所有控制台输出,然后缓冲并跟踪所有控制台输出,然后使用Console.SetCursorPositionConsole.Write重写它。

using System;
using System.IO;
using System.Text;

namespace ConsoleApp5
{
    public class ConsoleTruncator : TextWriter
    {
        TextWriter m_console;

        int m_maxlinelength;
        int m_columnposition;

        public ConsoleTruncator(int maxlinelength)
        {
            m_maxlinelength = maxlinelength;

            m_console = Console.Out; // save console stream reference between we replace it

            Console.SetOut(this); // redirect console to this TextWriter

            // (don't do the `SetOut` here ... you need to separate out the
            // the attach and detach of the custom `TextWriter` to the
            // console into some other utility class/helper...for good structure)
        }

        public override Encoding Encoding
        {
            get
            {
                return Encoding.UTF8;
            }
        }

        public override void Write(char value)
        {
            if (value == '\r')
                return;

            if (value == '\n')
            {
                OutputChar(value);

                m_columnposition = 0;
            }
            else
            {
                bool bRoom = (m_maxlinelength - m_columnposition) > 0;

                if (bRoom)
                {
                    OutputChar(value);

                    m_columnposition++;
                }
            }

        }

        private void OutputChar(char value)
        { 
            m_console.Write(value);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string[] somelines =
            {
                "AAALorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam auctor neque quis euismod auctor. Fusce massa nibh, fringilla eu mi ornare, ullamcorper tristique nibh. Aenean eget neque111",
                "BBBxxxxxxxx vvvvvvvvvvvvvvvvvvv xxxxxxxxxxxxxxxxx222",
                "CCCNullam porta eu magna eget rutrum. Aenean orci urna, pretium ut odio vitae, euismod rhoncus ligula. Vivamus condimentum semper lacus id tempor. Phasellus at turpis a quam ornare tincidunt. Vivamus eleifend quam eget velit porttitor, sit amet laoreet risus consectetur. Mauris eu arcu sit amet ipsum vulputate porttitor. Phasellus elementum erat eu blandit semper. Integer eu metus urna. Pellentesque at posuere purus. Nu333",
                "0123456789 0123456789 0123456789 0123456789 0123456789 0123456789\r\n0123456789+*+*+*+*+*0123456789+*+*+*+*+*0123456789",
                "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz\r\n\r\nABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            };

            using (var consoleTruncator = new ConsoleTruncator(40))
            {
                foreach(string s in somelines)
                {
                    Console.WriteLine(s);
                }

                Console.WriteLine();
                Console.Write("write a bit");
                Console.Write("-write a bit more");
                Console.Write("-write too muchhhhhhhhhhhhhhhh");
            }

            Console.ReadKey();
        }
    }
}

enter image description here

答案 2 :(得分:0)

您可以通过调整控制台窗口的屏幕缓冲区大小宽度来执行此操作:

enter image description here

要以编程方式进行调整,您可以致电:

 //Any value greater than 80:
 Console.BufferWidth = 120;