1

I have created a method that I'm using in a text based adventure. It works great, but the problem is that it splits words up when I want it to \n. I need to find a way for it to \n right after the last space before char 139 in the array. Here is my code:

    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);
        }
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jared Price
  • 5,217
  • 7
  • 44
  • 74

1 Answers1

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());
    }
corylulu
  • 3,449
  • 1
  • 19
  • 35