allow accented characters to be searchable? [duplicate]

99封情书 提交于 2019-12-14 01:54:30

问题


SOLVED:

I used the following code:

var compareinfo = CultureInfo.CurrentCulture.CompareInfo;
var index = compareinfo.IndexOf(strA, strB, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase);
return index > -1;

Possible Duplicate:
Ignoring accented letters in string comparison

I have a public Kiosk application where users use it to search for a place of interest. Say I have a shop name with the Café word. The kiosk only allows input of English alphabets through an on-screen keyboard. The problem is when I type in Cafe(without the accented é) the search is not valid because the user could not input the character é. I want the application to allow the normal e to be searchable to all accented e and likewise for all other respective characters. How can i do that?

EDIT: the shop name is "Bruce Café" and i search for "cafe" and it should show in my search results.

using

string.Compare("Bruce Café", "cafe", CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace)

returns -1

and

string.Compare("Ben-Gurion University (BGU)", "cafe", CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace)

also returns -1

which i don't know why is it so...


回答1:


If you're doing this in code:

string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);

and if you're doing this in T-SQL:

SELECT *
FROM YourTable
WHERE
   ColumnName COLLATE Latin1_general_CI_AI Like '%cafe%' COLLATE Latin1_general_CI_AI



回答2:


If you are doing your own string comparisons, then you can ignore the accents by specifying CompareOptions.IgnoreNonSpace to one of the string comparison methods that accepts a CompareOptions parameter, for example this String.Compare



来源:https://stackoverflow.com/questions/13340712/allow-accented-characters-to-be-searchable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!