How to check if string contains special characters in C#

1 Answer

0 votes
using System;

class Program
{
    static bool string_contains_special_characters(string str) {
        char[] SpecialChars = "!@#$%^&*()".ToCharArray();

        int indexOf = str.IndexOfAny(SpecialChars);
        
        if (indexOf == -1) {
            return false;
        }
            
        return true;
    }
    static void Main() {
        string str = "c# c++ java python";
        
        Console.Write(string_contains_special_characters(str));
    }
}



/*
run:

True

*/

 



answered Sep 4, 2023 by avibootz
edited Sep 4, 2023 by avibootz

Related questions

1 answer 226 views
1 answer 246 views
1 answer 329 views
1 answer 280 views
1 answer 215 views
1 answer 235 views
...