C# program to check if a string contains any special character

To check if a string contains any special character, you need to use the following method −


Use it inside for loop and check or the string that has special characters.

Let us say our string is −

string str = "Amit$#%";

Now convert the string into character array −


With that, use a for loop and to check for each character using the isLetterOrDigit() method.


Let us see the complete code.

Live Demo

using System;
namespace Demo {
   class myApplication {
      static void Main(string[] args) {
         string str = "Amit$#%";
         char[] one = str.ToCharArray();
         char[] two = new char[one.Length];
         int c = 0;
         for (int i = 0; i < one.Length; i++) {
            if (!Char.IsLetterOrDigit(one[i])) {
               two[c] = one[i];
         Array.Resize(ref two, c);
         Console.WriteLine("Following are the special characters:");
         foreach(var items in two) {


Following are the special characters:
Samual Sam
Samual Sam

Learning faster. Every day.