比较从 .txt 文件中拆分出来的字符串不起作用 Unity C#
Comparing strings that are split from .txt file not working Unity C#
我正在尝试为我们的游戏编写一个对话系统,我 运行 遇到了一个非常简单但烦人的问题。我将 .txt 文件中的文本行拆分为字符串数组。字符串本身工作得很好,但比较它们并不像通常那样工作。
下面的代码是我目前正在做的事情的简化版。它显示了我用来剪切字符串的语法以及我如何比较它们。就是拆分文本,回车得到下一段文本,遇到特定文本自动运行一段代码(本例为“-”)
所以问题是,Split 函数是否向字符串添加了一些东西,或者是否必须以不同的方式进行比较?
private string[] _textLines;
private TextAsset _textFile;
private int _currentLine;
private void PrepareText()
{
_textFile = _textLoader.GetTextAsset();
_textLines = (_textFile.text.Split('\n'));
private void update() {
if (Input.GetButtonDown("Continue"))
_currentLine += 1;
if (_textLines[_currentLine].Equals("-"))
DisableTextBox();
}
当我记录指定数组槽的内容时,它只给我应有的“-”,代码仍然认为它不相等。
我用于此测试的 .txt 文件如下所示。我只是想 运行 遇到第 4 行时的代码。
This is the first line
second line
third line, break coming
-
More dialog here
-
感谢您的快速提示!就像评论中建议的那样,使用以下代码向字符串添加一个不可见字符。
_textFile.text.Split('\n')
我没有像评论中建议的那样学习使用 StringReader 之类的新东西(因为我是那样的笨蛋),而是通过将 string.Equals() 的比较更改为 string.Contains() 并且它完美地工作(除了在我的情况下没有真正做任何事情的不可见字符)
private string[] _textLines;
private TextAsset _textFile;
private int _currentLine;
private void PrepareText()
{
_textFile = _textLoader.GetTextAsset();
_textLines = (_textFile.text.Split('\n'));
private void update() {
if (Input.GetButtonDown("Continue"))
_currentLine += 1;
if (_textLines[_currentLine].Contains("(end)"))
DisableTextBox();
}
对于我的示例 .txt 文件,必要的更改自然如下所示:
This is the first line
second line
third line, break coming
(end)
More dialog here
(end)
我建议您查看该讨论:Split text with '\r\n'
用 '\n' 分隔不会 "split" \r,因此数组中的所有值都以“\r”结尾。
我在上面提到的post中有不同的解决方案。
我正在尝试为我们的游戏编写一个对话系统,我 运行 遇到了一个非常简单但烦人的问题。我将 .txt 文件中的文本行拆分为字符串数组。字符串本身工作得很好,但比较它们并不像通常那样工作。
下面的代码是我目前正在做的事情的简化版。它显示了我用来剪切字符串的语法以及我如何比较它们。就是拆分文本,回车得到下一段文本,遇到特定文本自动运行一段代码(本例为“-”)
所以问题是,Split 函数是否向字符串添加了一些东西,或者是否必须以不同的方式进行比较?
private string[] _textLines;
private TextAsset _textFile;
private int _currentLine;
private void PrepareText()
{
_textFile = _textLoader.GetTextAsset();
_textLines = (_textFile.text.Split('\n'));
private void update() {
if (Input.GetButtonDown("Continue"))
_currentLine += 1;
if (_textLines[_currentLine].Equals("-"))
DisableTextBox();
}
当我记录指定数组槽的内容时,它只给我应有的“-”,代码仍然认为它不相等。
我用于此测试的 .txt 文件如下所示。我只是想 运行 遇到第 4 行时的代码。
This is the first line
second line
third line, break coming
-
More dialog here
-
感谢您的快速提示!就像评论中建议的那样,使用以下代码向字符串添加一个不可见字符。
_textFile.text.Split('\n')
我没有像评论中建议的那样学习使用 StringReader 之类的新东西(因为我是那样的笨蛋),而是通过将 string.Equals() 的比较更改为 string.Contains() 并且它完美地工作(除了在我的情况下没有真正做任何事情的不可见字符)
private string[] _textLines;
private TextAsset _textFile;
private int _currentLine;
private void PrepareText()
{
_textFile = _textLoader.GetTextAsset();
_textLines = (_textFile.text.Split('\n'));
private void update() {
if (Input.GetButtonDown("Continue"))
_currentLine += 1;
if (_textLines[_currentLine].Contains("(end)"))
DisableTextBox();
}
对于我的示例 .txt 文件,必要的更改自然如下所示:
This is the first line
second line
third line, break coming
(end)
More dialog here
(end)
我建议您查看该讨论:Split text with '\r\n'
用 '\n' 分隔不会 "split" \r,因此数组中的所有值都以“\r”结尾。
我在上面提到的post中有不同的解决方案。