C#IndexOf IgnoreCase Special Chars

时间:2015-05-08 14:56:19

标签: c#

如何使用特殊字符执行indexOf Case Insensite? 我需要搜索“benefícios”这个词,我希望IndexOf找到它,即使我不使用“i”上的特殊字符

这种方式我找不到它:

string str = "candidatar-se a benefícios";
string word = "beneficio";
str.IndexOf(word, StringComparison.OrdinalIgnoreCase)

但有了这个,我可以:

string str = "candidatar-se a benefícios exclusivos";
string word = "benefício";
str.IndexOf(word, StringComparison.OrdinalIgnoreCase)

由于

3 个答案:

答案 0 :(得分:5)

使用相关文化的class Foo { public: virtual void a()=0; }; class Bar:public Foo { public: void a(); // virtual }; class Baz1 : public Bar { public: void a(); // also virtual }; class Baz2 : public Bar { public: void a(int); // not virtual! }; 并使用CompareInfo

CompareOptions.IgnoreNonSpace

您还可以组合其他比较选项 - 如果您也想忽略大小写,例如,请使用string str = "candidatar-se a benefícios"; string word = "beneficio"; var compareInfo = CultureInfo.InvariantCulture.CompareInfo; compareInfo.IndexOf(str, word, CompareOptions.IgnoreNonSpace);

IndexOfCompareOptions的文档可能会有所帮助。

答案 1 :(得分:2)

查看this question

第二个答案似乎正是您所寻找的。

答案 2 :(得分:1)

你可以用两种方式:

要么使用CharUnicodeInfo.GetUnicodeCategory来表示变音符号:

$potatoes = $('.potatoes');

$potatoes.click( function() {
    $(this).fadeIn('fast'); // fade in the div that was clicked
    $potatoes.not( $(this) ).fadeOut('fast'); // fade out every other div
});

}

或您使用UnidecodeSharpFork https://bitbucket.org/DimaStefantsov/unidecodesharpfork

static string RemoveDiacritics(string text) 
{
    var normalizedString = text.Normalize(NormalizationForm.FormD);
    var stringBuilder = new StringBuilder();

    foreach (var c in normalizedString)
    {
        var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
        if (unicodeCategory != UnicodeCategory.NonSpacingMark)
        {
            stringBuilder.Append(c);
        }
    }

    return stringBuilder.ToString().Normalize(NormalizationForm.FormC);