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
Char.IsWhiteSpace() Method in C#
The Char.IsWhiteSpace() method in C# is used to determine whether a specified Unicode character is classified as white space. This includes spaces, tabs, line feeds, carriage returns, and other Unicode whitespace characters.
Syntax
Following is the syntax for the Char.IsWhiteSpace() method −
public static bool IsWhiteSpace(char ch);
Parameters
-
ch − The Unicode character to evaluate.
Return Value
Returns true if the character is a whitespace character; otherwise, false.
Using IsWhiteSpace() with Different Characters
Example
using System;
public class Demo {
public static void Main() {
bool res;
char val = ' ';
Console.WriteLine("Value = '" + val + "'");
res = Char.IsWhiteSpace(val);
Console.WriteLine("Is the value whitespace? = " + res);
}
}
The output of the above code is −
Value = ' ' Is the value whitespace? = True
Using IsWhiteSpace() with Non-Whitespace Characters
Example
using System;
public class Demo {
public static void Main() {
bool res;
char val = 'k';
Console.WriteLine("Value = " + val);
res = Char.IsWhiteSpace(val);
Console.WriteLine("Is the value whitespace? = " + res);
}
}
The output of the above code is −
Value = k Is the value whitespace? = False
Testing Multiple Whitespace Characters
Example
using System;
public class Demo {
public static void Main() {
char[] testChars = {' ', '\t', '<br>', '\r', 'A', '5', '@'};
Console.WriteLine("Testing various characters:");
foreach (char ch in testChars) {
string displayChar = ch == '\t' ? "\t" :
ch == '<br>' ? "\<br>" :
ch == '\r' ? "\r" : ch.ToString();
Console.WriteLine($"'{displayChar}' is whitespace: {Char.IsWhiteSpace(ch)}");
}
}
}
The output of the above code is −
Testing various characters: ' ' is whitespace: True '\t' is whitespace: True '<br>' is whitespace: True '\r' is whitespace: True 'A' is whitespace: False '5' is whitespace: False '@' is whitespace: False
Common Use Cases
The Char.IsWhiteSpace() method is commonly used for:
-
String parsing − Skipping whitespace characters while processing text.
-
Input validation − Checking if user input contains only whitespace.
-
Text formatting − Identifying whitespace for custom trimming operations.
Conclusion
The Char.IsWhiteSpace() method provides a reliable way to identify Unicode whitespace characters including spaces, tabs, and line breaks. It returns true for whitespace characters and false for all other characters, making it essential for text processing and validation tasks.
