比较两个相同的字符串但在C#中返回false。不知道为什么????

时间:2016-05-26 14:31:50

标签: c# c#-4.0 selenium-webdriver c#-3.0

     public bool VerifyTextPresent(By by, String actual)
    {
            WaitUntilElementIsPresent(by);
            String expected = GetText(by);
            return expected.Equals(actual);

    }

expected =“总胜利”

actual =“Total Win”

我也使用“Contains”方法但仅返回false。 请帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

  

我得到的东西就像我指出它的ascii值,实际空间值是160,预期空间值是32.但现在我怎么能继续前进?

一种方法是通过用基线替换某些字符来规范化字符串。在您的情况下,您可以使用" normal"替换不间断的空格。空间:

 public bool VerifyTextPresent(By by, String actual)
{
        WaitUntilElementIsPresent(by);
        String expected = GetText(by);

        if (expected.Equals(actual)) return true;
        if (expected.Equals(Normalize(actual))) return true;
        return false;
}

private string Normalize(string s)
{
    // hard-code for now; could use a lookup table or other means to expand  
    s = s.Replace((char)160, (char)32);
    // other replacements as necessary

    return s;

}  
相关问题