Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to check if a character is a whitespace character
In C#, a whitespace character includes spaces, tabs, newlines, and other invisible characters used for formatting text. The char.IsWhiteSpace() method provides an easy way to identify these characters programmatically.
Syntax
Following is the syntax for using char.IsWhiteSpace() method −
bool result = char.IsWhiteSpace(character);
Parameters
The method takes a single parameter −
- character − The character to be tested for whitespace.
Return Value
Returns true if the character is a whitespace character; otherwise, returns false.
Using char.IsWhiteSpace() with Different Whitespace Characters
The method recognizes various types of whitespace characters including space, tab, newline, and carriage return −
using System;
class Program {
static void Main() {
char space = ' ';
char tab = '\t';
char newline = '<br>';
char carriageReturn = '\r';
char letter = 'A';
Console.WriteLine("Testing different characters:");
Console.WriteLine($"Space ' ': {char.IsWhiteSpace(space)}");
Console.WriteLine($"Tab '\t': {char.IsWhiteSpace(tab)}");
Console.WriteLine($"Newline '\<br>': {char.IsWhiteSpace(newline)}");
Console.WriteLine($"Carriage Return '\r': {char.IsWhiteSpace(carriageReturn)}");
Console.WriteLine($"Letter 'A': {char.IsWhiteSpace(letter)}");
}
}
The output of the above code is −
Testing different characters: Space ' ': True Tab '\t': True Newline '<br>': True Carriage Return '\r': True Letter 'A': False
Using char.IsWhiteSpace() to Filter Strings
You can use the method to process strings and identify or remove whitespace characters −
using System;
class Program {
static void Main() {
string text = "Hello\tWorld<br>";
int whitespaceCount = 0;
Console.WriteLine("Original string: "" + text.Replace("\t", "\t").Replace("<br>", "\<br>") + """);
Console.WriteLine("Character analysis:");
for (int i = 0; i < text.Length; i++) {
char c = text[i];
if (char.IsWhiteSpace(c)) {
whitespaceCount++;
Console.WriteLine($"Position {i}: '{GetDisplayChar(c)}' - Whitespace");
} else {
Console.WriteLine($"Position {i}: '{c}' - Not whitespace");
}
}
Console.WriteLine($"Total whitespace characters: {whitespaceCount}");
}
static string GetDisplayChar(char c) {
switch (c) {
case ' ': return "space";
case '\t': return "tab";
case '<br>': return "newline";
case '\r': return "carriage return";
default: return c.ToString();
}
}
}
The output of the above code is −
Original string: "Hello\tWorld<br>" Character analysis: Position 0: 'H' - Not whitespace Position 1: 'e' - Not whitespace Position 2: 'l' - Not whitespace Position 3: 'l' - Not whitespace Position 4: 'o' - Not whitespace Position 5: 'tab' - Whitespace Position 6: 'W' - Not whitespace Position 7: 'o' - Not whitespace Position 8: 'r' - Not whitespace Position 9: 'l' - Not whitespace Position 10: 'd' - Not whitespace Position 11: 'newline' - Whitespace Total whitespace characters: 2
Practical Application: Counting Non-Whitespace Characters
Here's a practical example that counts only meaningful characters in a string −
using System;
class Program {
static void Main() {
string input = " Hello World! \t<br>";
int nonWhitespaceCount = 0;
Console.WriteLine("Input string: "" + input + """);
foreach (char c in input) {
if (!char.IsWhiteSpace(c)) {
nonWhitespaceCount++;
}
}
Console.WriteLine($"Total characters: {input.Length}");
Console.WriteLine($"Non-whitespace characters: {nonWhitespaceCount}");
Console.WriteLine($"Whitespace characters: {input.Length - nonWhitespaceCount}");
}
}
The output of the above code is −
Input string: " Hello World! " Total characters: 19 Non-whitespace characters: 12 Whitespace characters: 7
Conclusion
The char.IsWhiteSpace() method in C# provides a reliable way to identify whitespace characters including spaces, tabs, newlines, and carriage returns. This method is particularly useful for string processing, validation, and text parsing applications where you need to handle or filter whitespace characters.
