开发者

compare strings with one having nonreadable chars

开发者 https://www.devze.com 2023-03-04 00:47 出处:网络
I am having 开发者_运维知识库problem regarding comparing two strings. One string has one or more nonreadable characters in it while other other string is same but in readable format.

I am having 开发者_运维知识库problem regarding comparing two strings. One string has one or more nonreadable characters in it while other other string is same but in readable format.

When I try to use this, I am having trouble

if (Alemria=Almería)... 

I am having such string Almería in a table.

How can this be done?


Use an overload of string.Equals that takes a StringComparison enum - use one of the CurrentCulture enum members.

You will need to set the current culture to a culture that can sort by these characters.


Depending on how strict you want to be, you might try this. If you know the characters in question are only from spanish the alphabet you could strip those out of the seed values (maybe use RegEx) and modify your comparison logic to do the same with target records. For example, remove all 'ñ' and 'n' from both side and maybe add a length comparison to increase reliability. Of course do this with all the special characters, not just 'ñ'.


See if this article will help you, you could replace all accented characters in the word and then do your comparison.


I suppose CompareOptions.IgnoreNonSpace is that you are looking for. Compare will ignore accents, diacritics, and vowel marks.

        string str1 = "mun";
        string str2 = "mün";
        int result1 = string.Compare(str1, str2, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace);
        int result2 = string.Compare(str1, str2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);

But Alemria will differ from Almería anyway. Seems like it's considered totally another symbol.


Try

    if(var.StartsWith("Almer"))//repalce var with your string var
    MessageBox.Show("String matched");//do watever you want to do here
0

精彩评论

暂无评论...
验证码 换一张
取 消