1

我已经创建了一种我在基于文本的冒险中使用的方法。它工作得很好,但问题是当我想要它时它会拆分单词 \n。我需要找到一种方法让它在数组中 char 139 之前的最后一个空格之后 \n 。这是我的代码:

    static void RPGWrite(string write)
    {
        char[] writearray = write.ToCharArray();
        int writearraycount = writearray.Count();
        for (int x = 0; x < writearraycount; x++)
        {                
            Console.Write(Convert.ToString(writearray[x]));
            if ((x > 1) && (x % 139 == 0))
                Console.Write("\n");
            System.Threading.Thread.Sleep(30);
        }
    }
4

1 回答 1

0
    static void RPGWrite(string write)
    {
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (i < write.Length)
        {
            if(i + 139 < write.Length)
            { 
                string part = write.Substring(i, i + 139);
                int lastSpace = part.LastIndexOf(' ');
                sb.Append(write.Substring(i, i + lastSpace) + System.Environment.NewLine;
                i += lastSpace;
            }
            else
            {
                sb.Append(write.Substring(i));
                break;
            }
        }
        Console.Write(sb.ToString());
    }
于 2013-03-20T00:06:48.347 回答