public string OpenDialogueFile(string dialogueName) {
if(dialogues == null) {
dialogues = Resources.LoadAll<TextAsset>("Dialogue");
}
var text = "";
foreach(var ta in dialogues) {
print(ta.name + ".." + dialogueName);
if(ta.name == dialogueName) {
print("Found");
text = ta.text;
break;
}
}
return text;
}
此代码应找到具有所请求名称的纯文本文件并吐出内容。
使用Resources/Dialogue
中的文件“test1和test2”并请求test1,输出为
test1..test1
Found
但是,当请求 test2 时
test1..test2
test2..test2
程序声称 test2 不等于 test2。
ta.name和dialogName都是字符串,所以它应该通过内容来测试相等性。
我的相等操作有问题吗?
最佳答案
在比较字符串之前尝试清除字符串中的空格
var cleanDialogueName = dialogueName.Trim();
foreach(var ta in dialogues)
{
var cleanName = ta.name.Trim();
print(cleanName + ".." + cleanDialogueName);
if (cleanName == cleanDialogueName)
{
print("Found");
text = ta.text;
break;
}
}
关于c# - 内容相同的字符串不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23878289/