Char.IsWhiteSpace() Method in C#

In C# language, the "Char.IsWhiteSpace" method is a part of the System namespace and is used to check whether a specified Unicode character represents a white space character. White space Characters include spaces, tabs, line breaks, and other characters that separate words in a text.

Syntax:

It has the following syntax:

In this syntax, the character "c" is the Unicode character to evaluate.

It takes the character as a parameter, checks whether the given character is a white space, and returns a Boolean value. True represents that the given character is a white space, and False represents the given character is not a white space.

Example:

Character ' ' is white space: True

Character 'A' is white space: False

Character '5' is white space: False

Importance of IsWhiteSpace method:

Whitespace Detection: In this example, the primary importance of Char.IsWhiteSpace determines whether a given character is a white-space character. It is useful when dealing with text processing, where different behaviors or actions might be required based on the presence of whitespace.

Text Processing: In applications such as parsing, input validation, or formatting, knowing whether a character is a whitespace character helps in making decisions about how to handle or process that character in the context of the program.

Conditional Logic: The boolean results obtained from Char.IsWhiteSpace are used in conditional logic to make decisions or take actions based on whether a character is a white-space character or not.

Example:

Let us take a basic program in C# to illustrate the IsWhiteSpace() method.

Output:

Char.IsWhiteSpace() Method in C#

Explanation:

First, different characters representing space, tab, newline, the letter 'A' and the digit '5' are first value. After that, the "Char.IsWhiteSpace" method is used to check if each character is a white space character. Next, The boolean variables are assigned to the result of the method for each corresponding character. After that, the program prints the results, indicating whether each character is considered a white space character.

Example:

Let us take another C# program to illustrate the isWhiteSpace() method.

Output:

Char.IsWhiteSpace() Method in C#

Explanation:

In this program, the sentence is taken from the user as userInput, where the sentence will contain white spaces at the start, end, and between the words of the sentence. After that, the program calls the CountWhiteSpaces method by passing the user input as the argument. This method iterates through each character in the input and checks whether the character is a white space by using the IsWhiteSpace() method. If it is a white space character, it will increase the count, and this function will return the count of white space characters to the main function. The sentence will be trimmed, and then it will count the number of white spaces and again print the number of white space characters.

Char.IsWhiteSpace(String, Int32) Method

In C#, Char.IsWhiteSpace(String, Int32) check whether a character that is present at the given index in the string is a white space character or not. If there is a white space character at the given position in the string, it returns true; else, it returns false.

Syntax:

It has the following syntax:

It takes a string and integer as arguments representing the string where the function has to find the white character and position to check. It returns a Boolean value.

Example:

Let us take a C# program to illustrate the method IsWhiteSpace(char, int).

Output:

Char.IsWhiteSpace() Method in C#

Explanation:

This C# program prompts the user to input a string and an index. After that, it checks if the character at the specified index in the string is a white-space character using Char.IsWhiteSpace. The result and appropriate messages for invalid inputs or out-of-range indices are displayed. The program ensures the index is within the string's valid range and handles non-integer inputs gracefully.