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

To check if a string contains any special character in C#, you can use the Char.IsLetterOrDigit method. This method returns false for any character that is not a letter or digit, which means it's a special character.

Special characters include symbols like @, #, $, %, &, punctuation marks, and whitespace characters that are not alphanumeric.

Syntax

Following is the syntax for checking if a character is a letter or digit −

bool Char.IsLetterOrDigit(char c)

To check for special characters, use the negation operator −

if (!Char.IsLetterOrDigit(character)) {
   // character is a special character
}

Using Char.IsLetterOrDigit to Find Special Characters

The following example demonstrates how to identify and extract all special characters from a string −

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];
               c++;
            }
         }
         
         Array.Resize(ref two, c);
         Console.WriteLine("Following are the special characters:");
         foreach(var items in two) {
            Console.WriteLine(items);
         }
      }
   }
}

The output of the above code is −

Following are the special characters:
$
#
%

Using LINQ to Check for Special Characters

A more concise approach using LINQ to simply check if any special characters exist −

using System;
using System.Linq;

class Program {
   static void Main() {
      string str1 = "Hello123";
      string str2 = "Hello@123";
      
      bool hasSpecialChars1 = str1.Any(c => !Char.IsLetterOrDigit(c));
      bool hasSpecialChars2 = str2.Any(c => !Char.IsLetterOrDigit(c));
      
      Console.WriteLine($"'{str1}' has special characters: {hasSpecialChars1}");
      Console.WriteLine($"'{str2}' has special characters: {hasSpecialChars2}");
   }
}

The output of the above code is −

'Hello123' has special characters: False
'Hello@123' has special characters: True

Using Regular Expressions

You can also use regular expressions to check for special characters −

using System;
using System.Text.RegularExpressions;

class Program {
   static void Main() {
      string str = "Password123!";
      
      // Pattern matches any character that is NOT alphanumeric
      bool hasSpecialChars = Regex.IsMatch(str, @"[^a-zA-Z0-9]");
      
      Console.WriteLine($"'{str}' contains special characters: {hasSpecialChars}");
      
      // Find all special characters
      MatchCollection matches = Regex.Matches(str, @"[^a-zA-Z0-9]");
      Console.WriteLine("Special characters found:");
      foreach (Match match in matches) {
         Console.WriteLine(match.Value);
      }
   }
}

The output of the above code is −

'Password123!' contains special characters: True
Special characters found:
!

Comparison of Different Approaches

Method Pros Cons
Char.IsLetterOrDigit Built-in, fast, handles Unicode correctly Requires loop for string iteration
LINQ Any() Concise, readable, stops at first match Requires System.Linq namespace
Regular Expressions Flexible pattern matching, powerful Slower performance, more complex

Conclusion

The Char.IsLetterOrDigit method is the most efficient way to check for special characters in C#. Use LINQ's Any() method for a more concise approach when you only need to know if special characters exist, and regular expressions when you need complex pattern matching capabilities.

Updated on: 2026-03-17T07:04:35+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements