

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
Char.IsLetterOrDigit
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 −
str.ToCharArray();
With that, use a for loop and to check for each character using the isLetterOrDigit() method.
Example
Let us see the complete code.
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); } Console.ReadLine(); } } }
Output
Following are the special characters: $ # %
- Related Questions & Answers
- Java program to check if a string contains any special character
- Program to check if a string contains any special character in C
- Program to check if a string contains any special character in Python
- Python program to check if a string contains any unique character
- PHP program to check if a string has a special character
- Check if string contains special characters in Swift
- Java Program to check if the String contains any character in the given set of characters
- Java Program to Check if a string contains a substring
- Python program to check if a string contains all unique characters
- How to check if a string contains only one type of character in R?
- C# Program to replace a special character from a String
- How to check if a string contains a specific sub string?
- Check if a string contains a sub-string in C++
- Check if a string contains numbers in MySQL?
- MySQL query to check if a string contains a word?
Advertisements