首页 > 解决方案 > 让 C# 控制台应用程序包装完整的单词而不是拆分它们

问题描述

我使用了另一个问题中的一些代码来获得自动换行来影响整个单词而不是拆分它们。

我想包含其他包含格式的字符串,但无法弄清楚或在 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();
}

非常感谢任何帮助,尽管我仍在学习,但请对我温柔一点。外行的条款会非常有帮助:)

标签: c#.netstringconsoleformatting

解决方案


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

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

你看我们写一行,改变颜色,再写一行。我们必须手动结束段落,而不是作为文本块的一部分。

还有其他策略,但这个策略允许使用您的大部分代码。


推荐阅读