
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- 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
- Java Program to check if the String contains any character in the given set of characters
- Check if string contains special characters in Swift
- JAVA Menu Driven Program to Check Character is String, Number or Special Character
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- C# Program to replace a special character from a String
- How to check if a string contains only one type of character in R?
- Python program to check if a string contains all unique characters
- How to check if a string contains a specific sub string?
- Java Program to check if the String contains only certain characters

Advertisements