具有相同内容的字符串不相等?

时间:2022-02-21 11:46:46
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;
}

This code should find a plain text file with the name requested and spit out the contents.

此代码应找到具有所请求名称的纯文本文件并吐出内容。

With the files "test1 and test2" in Resources/Dialogue and requesting test1, the output is

使用Resources / Dialogue中的文件“test1和test2”并请求test1,输出为

test1..test1
Found

When test2 is requested, however

但是,当请求test2时

test1..test2
test2..test2

The program claims that test2 is unequal to test2.

该程序声称test2不等于test2。

Both ta.name and dialogueName are strings, so it should be testing equality by content.

ta.name和dialogName都是字符串,因此它应该按内容测试相等性。

Is there something wrong with my equality operation?

我的平等操作有问题吗?

3 个解决方案

#1


3  

Try cleaning your strings from whitespaces before comparing them

在比较它们之前,尝试从空格中清除字符串

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;
    }
 }

#2


1  

You are right, "test2" should equal to "test2". Convert the strings to char arrays with the string.ToCharArray() method to detect the problem. Check the number of characters and their value. Maybe it is an Encoding problem.

你是对的,“test2”应该等于“test2”。使用string.ToCharArray()方法将字符串转换为char数组以检测问题。检查字符数及其值。也许这是一个编码问题。

#3


0  

Instead of using the == Operator it is recommended to use the .equals method of the String class to compare Strings for equality.

不使用==运算符,建议使用String类的.equals方法来比较Strings的相等性。

For more inforation please refer to the msdn documentation or this * post

有关更多信息,请参阅msdn文档或此*帖子

Using .equals instead of == should resolve your problem without having to resort to crazy hackish workarounds

使用.equals而不是==应该解决您的问题,而不必诉诸疯狂的hackish变通办法

#1


3  

Try cleaning your strings from whitespaces before comparing them

在比较它们之前,尝试从空格中清除字符串

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;
    }
 }

#2


1  

You are right, "test2" should equal to "test2". Convert the strings to char arrays with the string.ToCharArray() method to detect the problem. Check the number of characters and their value. Maybe it is an Encoding problem.

你是对的,“test2”应该等于“test2”。使用string.ToCharArray()方法将字符串转换为char数组以检测问题。检查字符数及其值。也许这是一个编码问题。

#3


0  

Instead of using the == Operator it is recommended to use the .equals method of the String class to compare Strings for equality.

不使用==运算符,建议使用String类的.equals方法来比较Strings的相等性。

For more inforation please refer to the msdn documentation or this * post

有关更多信息,请参阅msdn文档或此*帖子

Using .equals instead of == should resolve your problem without having to resort to crazy hackish workarounds

使用.equals而不是==应该解决您的问题,而不必诉诸疯狂的hackish变通办法