我创建了一个我在基于文本的冒险中使用的方法。它工作得很好,但问题是它在我想要它时将它分开。我需要在数组中的char 139之前的最后一个空格之后找到它的方法。 这是我的代码:
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);
}
}
答案 0 :(得分: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());
}