Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图制作一个文本解析器,在需要的地方插入换行符,但它';它不太管用_C#_Text Parsing - Fatal编程技术网

C# 试图制作一个文本解析器,在需要的地方插入换行符,但它';它不太管用

C# 试图制作一个文本解析器,在需要的地方插入换行符,但它';它不太管用,c#,text-parsing,C#,Text Parsing,我正在做一个游戏,我从一个XML文件中读取对话文本。我正在尝试创建一个例程,根据需要自动添加换行符,以便将其放入文本框中。不过,这样做是行不通的。以下是当前的代码: SpriteFont dialogueFont = font31Adelon; int lastSpace = 0; string builder = ""; string newestLine = ""; float maxWidth = 921.6f; float stringLength = 0; for (int i =

我正在做一个游戏,我从一个XML文件中读取对话文本。我正在尝试创建一个例程,根据需要自动添加换行符,以便将其放入文本框中。不过,这样做是行不通的。以下是当前的代码:

SpriteFont dialogueFont = font31Adelon;
int lastSpace = 0;
string builder = "";
string newestLine = "";
float maxWidth = 921.6f;
float stringLength = 0;

for (int i = 0; i <= speech.Length - 1; i++) //each char in the string
{
    if (speech[i] == ' ') //record the index of the most recent space
    {
        lastSpace = i;
    }

    builder += speech[i]; 
    newestLine += speech[i]; 
    stringLength = dialogueFont.MeasureString(newestLine).X;

    if (stringLength > maxWidth) //longer than allowed
    {
        builder = builder.Remove(lastSpace); //cut off from last space 
        builder += Environment.NewLine;
        i = lastSpace; //start back from the cutoff
        newestLine = "";
    }
}
speech = builder;
SpriteFont dialogueFont=font31Adelon;
int lastSpace=0;
字符串生成器=”;
字符串newestLine=“”;
浮动最大宽度=921.6f;
浮动长度=0;
对于(int i=0;i maxWidth)//长度超过允许值
{
builder=builder.Remove(lastSpace);//从最后一个空间切断
builder+=Environment.NewLine;
i=lastSpace;//从截止点返回
newestLine=“”;
}
}
语言=建设者;
我的测试字符串是“这是一个长演讲的示例,必须正确地分成多行。它有几行长,实际上没有说任何重要的内容,因为它只是示例文本。”

这就是演讲的结局:

我认为,第一行之所以有效,是因为它恰好是一个空间,使它超越了极限

i=81,lastSpace=80是第二行的终点。生成器在.Remove命令之前如下所示: “这是一个将被分成多行的长讲话示例\r\n c”

运行后,它看起来如下所示: “这是一个长讲话的示例,\r\n需要分成多行”

第三行超出了i=123和lastSpace=120的大小限制。在删除之前,看起来是这样的。删除: “这是一个长讲话的示例,\r\n它被正确地分解为多行。\r\n它有几行长,而且是doe”

及之后: “这是一个长讲话的示例,\r\n需要正确地分成多行。它有几行长”

正如你所看到的,它切断了一个额外的字符,即使字符80,这个空格,是它应该开始删除的地方。从我所读到的内容来看.Remove,当使用单个参数调用时,会删除包括给定索引和之后的所有内容。不过,它也在减少i=79!似乎从lastSpace中加或减来弥补这一点应该很容易,但我要么得到“索引越界”错误,要么剪掉更多字符。我已经尝试过删除(lastSpace,I-lastSpace),但这也不起作用。我尝试过用不同的方式处理“以空格结尾”的案例,从lastSpace中进行增减。我尝试过用不同的方式来分解事物,但都没有成功


我已经看腻了,如果有任何帮助,我将不胜感激。

作为一种替代方法,您可以使用.split将句子分成一个数组,然后填充方框,直到没有空间进行下一步工作,然后添加换行符并开始下一行

> p> >将代码>环境.NeXLe> <代码>添加到<代码> StringBuilder < /C>中,当指定索引开始删除时,需要考虑这一点。
环境。换行符的值取决于系统。它可以是
“\r\n”
“\n”
“\r”
(或仅前两个中的一个)。
在您的例子中,它是
“\r\n”
,这意味着要删除一个空格,您添加了另外两个字符

首先,您需要声明一个新变量:

int additionalChars = 0;
然后,在添加一行文本时,您应该将代码更改为以下内容:

builder = builder.Remove(lastSpace + additionalChars); //cut off from last space 
builder += Environment.NewLine;
additionalChars += Environment.NewLine.Length - 1; 
-1
是因为您已经删除了一个空格(应使此代码独立于系统对
环境的定义。换行符

更新:您还应说明长度超过行限制的单词。无论如何,你应该打破它们(情不自禁地尝试):


您可以执行类似以下代码的操作吗。好处是双重的。首先,它跳转到下一个空格来测量和决定是否添加整个单词,而不是逐个字母。其次,它只为字符串中的每个单词调用一次MeasureString,而不是为添加到字符串中的每个字母调用一次MeasureString。当单词合适时,它使用带
Append
的StringBuilder;当单词不合适且需要添加新行时,它使用带
AppendLine
的StringBuilder

        int lastSpace = 0;
        int nextSpace = s.IndexOf(' ', lastSpace + 1);
        float width = 0;
        float totalWidth = 0;
        float maxWidth = 200;
        while (nextSpace >= 0)
        {
            string piece = s.Substring(lastSpace, nextSpace - lastSpace);
            width = g.MeasureString(piece, this.Font).Width;
            if (totalWidth + width < maxWidth)
            {
                sb.Append(piece);
                totalWidth += width;
            }
            else
            {
                sb.AppendLine(piece);
                totalWidth = 0;
            }
            lastSpace = nextSpace;
            nextSpace = s.IndexOf(' ', lastSpace + 1);
        }
        MessageBox.Show(sb.ToString());
int lastSpace=0;
int nextSpace=s.IndexOf(“”,lastSpace+1);
浮动宽度=0;
浮动总宽度=0;
浮动最大宽度=200;
while(nextSpace>=0)
{
弦段=s.Substring(lastSpace,nextSpace-lastSpace);
宽度=g.MeasureString(件,此字体)。宽度;
if(总宽度+宽度<最大宽度)
{
某人追加(件);
总宽度+=宽度;
}
其他的
{
某人追加(件);
总宽度=0;
}
lastSpace=nextSpace;
nextSpace=s.IndexOf(“”,lastSpace+1);
}
Show(sb.ToString());

好的,这让我现在很紧张。我猜我一直认为新行中的角色“不在那里”?我现在觉得有点傻,但非常感谢。
        int lastSpace = 0;
        int nextSpace = s.IndexOf(' ', lastSpace + 1);
        float width = 0;
        float totalWidth = 0;
        float maxWidth = 200;
        while (nextSpace >= 0)
        {
            string piece = s.Substring(lastSpace, nextSpace - lastSpace);
            width = g.MeasureString(piece, this.Font).Width;
            if (totalWidth + width < maxWidth)
            {
                sb.Append(piece);
                totalWidth += width;
            }
            else
            {
                sb.AppendLine(piece);
                totalWidth = 0;
            }
            lastSpace = nextSpace;
            nextSpace = s.IndexOf(' ', lastSpace + 1);
        }
        MessageBox.Show(sb.ToString());