How to find whether a string contains any of the special characters?

后端 未结 10 1291
春和景丽
春和景丽 2020-12-10 01:43

I want to find whether a string contains any of the special characters like !,@,#,$,%,^,&,*,(,)....etc.

How can I do that without looping thorugh all the charact

10条回答
  •  遥遥无期
    2020-12-10 02:06

    //apart from regex we can also use this
    string input = Console.ReadLine();
                char[] a = input.ToCharArray();
                char[] b = new char[a.Length];
                int count = 0;
                for (int i = 0; i < a.Length; i++)
                {
                    if (!Char.IsLetterOrDigit(a[i]))
                    {
                        b[count] = a[i];
                        count++;
                    }
                }
                Array.Resize(ref b, count);
                foreach(var items in b)
                {
                    Console.WriteLine(items);
                }
                Console.ReadLine();
    //it will display the special characters in the string input
    

提交回复
热议问题