4

My example code:

public static bool StringSearchHit(string keyWord, params string[] targetArray)
{
    foreach (string targetItem in targetArray)
    {
        if (targetItem.IndexOf(keyWord, StringComparison.InvariantCultureIgnoreCase) >= 0)
            return true;
    }
    return false;
}

The problem I have: the LIKE operator in SQL is not just string.Contain() or string.IndexOf(), there are "culture character similarity" in it (I don't know how to call it).

Example: when I search using LIKE '%pokemon%', the "Pokémon" will also be included. The character "é" will be considered as similar to "e".

Same for some German characters. The special characters "ä ö ü" will also be considered "like" "a o u" in SQL.

My goal: when the keyWord is "aou", and one of the targetArray has "äöü", it will also return true.

Is there any equivalent of this in c#?

Edit: thanks for pointing out about another post:

Ignoring accented letters in string comparison

That is the correct direction, but in another issue, it's "compare", not "contain". How to do similar thing in "string.Contain"?

Edit 2: Thanks for the correct answer:

Complex "Contains" string comparison

Tried this solution, "Pokémon" can be searched using "Pokemon", and "Æon Flux" can be searched using "aeon".

Case close and marked as duplicate.

cdytoby
  • 839
  • 10
  • 26
  • Duplicate of https://stackoverflow.com/questions/835790/how-to-do-sql-like-in-linq. In particular this answer is good (uses `SqlMethods.Like()`): https://stackoverflow.com/a/3045735/1447389 – Soleil Jan 10 '19 at 20:01
  • @GSerg Thanks, but I still can't find the answer in the other post. `RemoveAccents` looks good but I don't know which target accent the string has. I can't just use "iso-8859-8" hardcoded. – cdytoby Jan 10 '19 at 20:01
  • 3
    Ah, it's about `Contains`, my bad. Possible duplicate of [Complex “Contains” string comparison](https://stackoverflow.com/q/15178922/11683) – GSerg Jan 10 '19 at 20:02
  • Try Contains() method – Jonathan Applebaum Jan 10 '19 at 20:04
  • 2
    @Soleil `SqlMethods.Like` is for building an SQL query with a C# lambda. There is no SQL context here, the source data is given as a C# string array. – GSerg Jan 10 '19 at 20:06
  • @Soleil , not a duplicate, GSerg was closer. Please read problem better. – Aldert Jan 10 '19 at 20:09
  • @GSerg Thanks, your second duplicate suggestion really solves my problem. How do I mark this as duplicate with your second post link? – cdytoby Jan 10 '19 at 20:17

0 Answers0