Check if a string contains one of 10 characters

前端 未结 6 915
情话喂你
情话喂你 2020-12-04 12:01

I\'m using C# and I want to check if a string contains one of ten characters, *, &, # etc etc.

What is the best way?

6条回答
  •  [愿得一人]
    2020-12-04 12:22

    Thanks to all of you! (And Mainly Jon!): This allowed me to write this:

        private static readonly char[] Punctuation = "$€£".ToCharArray();
    
        public static bool IsPrice(this string text)
        {
            return text.IndexOfAny(Punctuation) >= 0;
        }
    

    as I was searching for a good way to detect if a certain string was actually a price or a sentence, like 'Too low to display'.

提交回复
热议问题