为什么这个c#字符串包装算法不起作用?

时间:2016-06-22 00:45:45

标签: c# string console cosmos

好吧,所以,在我的C#Cosmos操作系统中,我正在使用一个允许我输入字符串和所需宽度的系统,并根据宽度将字符串包装成行。

它的工作原理如下:输入字符串是" Hello Hello world"。宽度为6.算法将遍历字符串,如果char索引是宽度的索引,并且当前char是空格,则它将从字符串的开头到该点采取所有内容,添加它到List,并从字符串本身中删除它,并将char索引重置为0,然后重新开始。它执行此操作直到字符串为空或小于宽度。如果它小于宽度,则将其添加到List中,并且for循环终止。在非专业人士中,我们的输出字符串应如下所示:

您好
美丽
世界。

这是我的代码。

public static List<string> split_string(int width, string text)
    {
        List<string> text_lines = new List<string>();
        //Separate lines of text.
        for (int i = 0; i < text.Length; i++)
        {
            if (text.Length <= width)
            {
                text_lines.Add(text);
                i = text.Length + 5;
            }
            else
            {
                if (i >= width)
                {
                    if (text[i] == ' ')
                    {
                        text_lines.Add(text.Substring(0, i + 1));
                        text = text.Remove(0, i + 1);
                        i = 0;
                    }
                }
            }
        }
        return text_lines;
    }

事情是,有时,如果我最终不得不处理小于宽度的字符串,我们就会遇到问题。它似乎跳过了字符串的那一部分。糟糕!

例如,这是我使用此操作系统的操作系统。它应该采用标题和消息,然后使用OK按钮将其显示在消息框中。

public static void ShowMessagebox(string title, string text)
    {
        int splitWidth = 25;
        if(text.Length < splitWidth)
        {
            splitWidth = text.Length;
        }
        if(title.Length > splitWidth)
        {
            splitWidth = title.Length;
        }
        var lines = new List<string>();
        if(splitWidth > text.Length)
        {
            lines.Add(text);
        }
        else
        {
            lines = TUI.Utils.split_string(splitWidth, text);
        }
        foreach(var line in lines)
        {
            if(text.Contains(line))
            {
                text = text.Replace(line, "");
            }
        }
        if(text.Length > 0)
        {
            lines.Add(text);
        }
        int h = lines.Count + 4;
        int w = 0;
        foreach(var line in lines)
        {
            if(line.Length + 4 > w)
            {
                w = line.Length + 4;
            }
        }
        int x = (Console.WindowWidth - w) / 2;
        int y = (Console.WindowHeight - h) / 2;
        TUI.Utils.ClearArea(x, y, w, h, ConsoleColor.Green);
        TUI.Utils.ClearArea(x, y, w, 1, ConsoleColor.White);
        TUI.Utils.Write(x + 1, y, title, ConsoleColor.White, ConsoleColor.Black);
        for(int i = 0; i < lines.Count - 1; i++)
        {
            TUI.Utils.Write(x + 2, (y + 2) + i, lines[i], ConsoleColor.Green, ConsoleColor.White);
        }
        int xw = x + w;
        int yh = y + h;
        TUI.Utils.Write(xw - 6, yh - 2, "<OK>", TUI.Utils.COL_BUTTON_SELECTED, TUI.Utils.COL_BUTTON_TEXT);
        bool stuck = true;
        while (stuck)
        {
            var kinf = Console.ReadKey();
            if (kinf.Key == ConsoleKey.Enter)
            {
                stuck = false;
                Console.Clear();
            }
            else
            {

            }
        }
    }

非常简单。以默认宽度25个字符开始,如果标题较大,则将其设置为标题长度。如果文本长度小于宽度,则设置宽度以进行补偿。然后它从上面调用分割器算法,在&#39; TUI.Utils&#39;中找到,然后做一些东西打印到屏幕上。

这是我的操作系统&#34; ConfigurationManager&#34;,一个接收用户输入并使用它来生成配置文件的应用程序。现在正在进行中。

                    Curse.ShowMessagebox("Memphis can't run properly this system.", "Memphis needs at least one FAT partition on a Master Boot Record to be able to store it's configuration and other files on. Please use a partition utility like GParted to partition your hard drive properly.");

但看看屏幕上出现的内容......

The messagebox coming out of the above method call

正如你所看到的,并不是我想要的东西。它缺少一些字符串!

1 个答案:

答案 0 :(得分:2)

您不需要更改text,因为我们可以存储原始子字符串的偏移量。我们做的字符串操作越少越好。

public static List<string> split_string(int width, string text)
{
    width = width - 1; //So we're not constantly comparing to width - 1
    var returnSet = new List<string>();
    var currLength = 0;
    var oldOffset = 0;
    for (var i = 0; i < text.Length; i++)
    {
        if (currLength >= width && text[i] == ' ')
        {
            returnSet.Add(text.Substring(oldOffset, i - oldOffset));
            oldOffset = i + 1;
            currLength = 0;
            continue;
        }
        currLength++;
    }
    if (oldOffset < text.Length)
        returnSet.Add(text.Substring(oldOffset));

    return returnSet;
}

测试:

split_string(25, "Memphis needs at least one FAT partition on a Master Boot Record to be able to store it's configuration and other files on. Please use a partition utility like GParted to partition your hard drive properly.");

给出:

Memphis needs at least one 
FAT partition on a Master 
Boot Record to be able to 
store it's configuration 
and other files on. Please 
use a partition utility like 
GParted to partition your 
hard drive properly. 
split_string(6, "Hello beautiful world.")

给出

Hello 
beautiful 
world.