Case insensitive string.Contains in C#

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024
  • Checking for a substring that is case insensitive is not as simple as it first appears. To ensure your code is robust you need to take into account the culture of the data source, because different languages use different rules for handling case sensitivity.
    C# Answers provides answers to common questions about the C# language and the related .NET system. Do you have a programming question about C# or .NET that you would like answered?
    Add it as a comment below and I try to answer them all!
    GitHub: github.com/Com...

Комментарии • 2

  • @lumeronswift
    @lumeronswift Год назад

    If you're looking at a multilingual/cultural dataset, is it possible to set the culture/region based on the characters used in the query, or does it have to be explicitly defined (i.e. via something like a dropdown if exposed to an end user)?
    If automatic is possible, would there be any issue with language crossover (such as Polish, which has some different characters but shares many with English)?
    Is there any way to use this functionality in a situation where you are not sure which language the query/filter is written in (like with Polish)?

    • @EverythingCSharp
      @EverythingCSharp  Год назад

      You need to know the culture of the data for the customer. Maybe when the customer creates an account you get them to specify the country they are from. Or maybe you already know and can store that along with the customer details. Then whenever you do the string operation for a user, you can pull the culture they are from and use that. I am not aware of any way to automatically determine the culture from a string.