使C#控制台应用程序包装完整的单词而不是拆分它们

时间:2019-03-15 04:42:48

标签: c# .net string console formatting

我使用了another question中的一些代码来进行自动换行,以影响整个单词而不是将它们拆分。

我希望包含其他包含格式的字符串,但无法弄清楚它或在Google机器上找不到任何内容。

请参见下面的代码。

using System;
using System.Collections.Generic;

/// <summary>
///     Writes the specified data, followed by the current line terminator, 
///     to the standard output stream, while wrapping lines that would otherwise 
///     break words.
/// </summary>
/// <param name="paragraph">The value to write.</param>
/// <param name="tabSize">The value that indicates the column width of tab 
///   characters.</param>
public static void WordWrap(string paragraph, int tabSize = 8)
{
   string[] lines = paragraph
               .Replace("\t", new String(' ', tabSize))
               .Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

   for (int i = 0; i < lines.Length; i++) {
        string process = lines[i];
        List<String> wrapped = new List<string>();

        while (process.Length > Console.WindowWidth) {
            int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1, process.Length));
            if (wrapAt <= 0) break;

            wrapped.Add(process.Substring(0, wrapAt));
            process = process.Remove(0, wrapAt + 1);
        }

        foreach (string wrap in wrapped) {
            Console.WriteLine(wrap);
        }

        Console.WriteLine(process);
    }
}

我想使用的格式只是在有人讲话时更改对话的颜色,或更改某些关键字(地名,项目和字符名等)的颜色。

请参见下面的代码。

public static void townName(string town)
{
    Console.ForegroundColor = ConsoleColor.Magenta;
    Game.WordWrap(town);
    Console.ResetColor();
}

public static void Dialog(string message)
{
    Console.ForegroundColor = ConsoleColor.DarkCyan;
    Game.WordWrap(message);
    Console.ResetColor();
}

public static void Villain()
{
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Game.WordWrap("Zanbar Bone");
    Console.ResetColor();
}

任何帮助都将不胜感激,尽管我仍在学习,但请对我保持柔和。莱曼的条款会很有帮助:)

1 个答案:

答案 0 :(得分:0)

因此,我们将根据需要逐行编写颜色,然后手动结束段落。

    //keep track of the end width right here
    static int endWidth = 0;
    public static void WordWrap(string paragraph, int tabSize = 8)
    {
        //were only doing one bit at a time
        string process = paragraph;
        List<String> wrapped = new List<string>();

        //if were going to pass the end
        while (process.Length + endWidth > Console.WindowWidth)
        {
            //reduce the wrapping in the first line by the ending with
            int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1 - endWidth, process.Length));

            //if there's no space
            if (wrapAt == -1)
            {
                //if the next bit won't take up the whole next line
                if (process.Length < Console.WindowWidth - 1)
                {
                    //this will give us a new line
                    wrapped.Add("");
                    //reset the width
                    endWidth = 0;
                    //stop looping
                    break;
                }
                else
                {
                    //otherwise just wrap the max possible
                    wrapAt = Console.WindowWidth - 1 - endWidth;
                }
            }

            //add the next string as normal
            wrapped.Add(process.Substring(0, wrapAt));

            //shorten the process string
            process = process.Remove(0, wrapAt + 1);

            //now reset that to zero for any other line in this group
            endWidth = 0;
        }

        //write a line for each wrapped line
        foreach (string wrap in wrapped)
        {
            Console.WriteLine(wrap);

        }

        //don't write line, just write. You can add a new line later if you need it, 
        //but if you do, reset endWidth to zero
        Console.Write(process);

        //endWidth will now be the lenght of the last line.
        //if this didn't go to another line, you need to add the old endWidth
        endWidth = process.Length + endWidth;

    }


    //use this to end a paragraph
    static void EndParagraph()
    {
        Console.WriteLine();
        endWidth = 0;
    }

以下是使用此示例:

        Console.BackgroundColor = ConsoleColor.Blue;
        WordWrap("First Line. ");
        Console.BackgroundColor = ConsoleColor.Red;
        WordWrap("This is a much longer line. There are many lines like it but this one is my line "+  
            "which is a very long line that wraps.");
        EndParagraph();
        WordWrap("Next Paragraph");

您会看到我们写了一行,更改了颜色,然后写了另一行。我们必须手动结束段落,而不是将其作为文本块的一部分。

还有其他策略,但是这种策略允许使用大多数代码。