1

How can i compare this two value? is there any idea?

Please look at this image and tell me what can i do?

enter image description here

1- The code

2- variable values and it seams the same!

3- The output that shows it is not OK! and these values are not the same!

4- I understand that the only different between these two values are in 2 bytes of them. (I use this link for getting byte[] of string with UTF8 property)

Solution:

I wrote this extension method to solve the problem:

public static class FarsiExtension
{
    public static string FixYEH(this String instance)
    {
        return instance.Replace('ي', 'ی');
    }
}

and you can use it this way:

string fixedValue = stateKhedmat.ToString().FixYEH();
Community
  • 1
  • 1

3 Answers3

0

That they look like the same character doesn't mean it is the same character, as you see in the byte code.

I figure the only way to make these strings match is to always normalize the strings that you get by replacing 1 with the other.

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
0

I think you're have padding issues, convert both string to bytes and remove padding and convert back to string. this should solve this problem.

0

Seems to work with CompareOptions.IgnoreNonSpace, but I am not sure how to test for false positives:

string s1 = "پایان خدمت", s2 = "پايان خدمت"; char c1 = 'ی', c2 = 'ي';
var ci = System.Globalization.CultureInfo.InvariantCulture.CompareInfo;

Debug.Print(ci.Compare(s1, s2, CompareOptions.IgnoreNonSpace) + ""); // 0
Debug.Print(ci.Compare(s1, s2                               ) + ""); // 1

Debug.Print(ci.IndexOf(s1, c1, CompareOptions.IgnoreNonSpace) + ""); // 2
Debug.Print(ci.IndexOf(s1, c1                               ) + ""); // 2

Debug.Print(ci.IndexOf(s1, c2, CompareOptions.IgnoreNonSpace) + ""); // 2
Debug.Print(ci.IndexOf(s1, c2                               ) + ""); // -1
Slai
  • 22,144
  • 5
  • 45
  • 53