TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# char.IsControl Use

This C# example program tests the char.IsControl method. It finds all control characters.

Char.IsControl. The char.IsControl method detects control characters.

Certain characters do not represent a letter. Instead a char can represent a vertical space or even a beep. IsControl detects these codes.

Note: The char.IsControl method returns true or false depending on if the character is a non-printing character.

Example. The program illustrates the usage of the char.IsControl method. It contains a loop through all the possible values of a char data type. Those characters are tested to see if they are control characters with IsControl.

Then: The char.IsControl overload that accepts two arguments—a string and an index—is tested.

Overload Method

C# program that tests IsControl method

using System;

class Program
{
    static void Main()
    {
	// Loop through all possible char values and test for control.
	for (int i = 0; i < char.MaxValue; i++)
	{
	    char c = (char)i;
	    if (char.IsControl(c))
	    {
		Console.WriteLine("Is control: {0}", i);
	    }
	}
	// See if carriage return is a control character.
	Console.WriteLine("Is control: {0}", char.IsControl("\r\n", 0));
    }
}

Output

Is control: 0
Is control: 1
Is control: 2
Is control: 3
Is control: 4
Is control: 5
Is control: 6
Is control: 7
Is control: 8
Is control: 9
Is control: 10
Is control: 11
Is control: 12
Is control: 13
Is control: 14
Is control: 15
Is control: 16
Is control: 17
Is control: 18
Is control: 19
Is control: 20
Is control: 21
Is control: 22
Is control: 23
Is control: 24
Is control: 25
Is control: 26
Is control: 27
Is control: 28
Is control: 29
Is control: 30
Is control: 31
Is control: 127
Is control: 128
Is control: 129
Is control: 130
Is control: 131
Is control: 132
Is control: 133
Is control: 134
Is control: 135
Is control: 136
Is control: 137
Is control: 138
Is control: 139
Is control: 140
Is control: 141
Is control: 142
Is control: 143
Is control: 144
Is control: 145
Is control: 146
Is control: 147
Is control: 148
Is control: 149
Is control: 150
Is control: 151
Is control: 152
Is control: 153
Is control: 154
Is control: 155
Is control: 156
Is control: 157
Is control: 158
Is control: 159
Is control: True

We see the control characters in the character set in the char type are equal to the decimal representations of 0-31, and 127-159. The characters \r and \n are equal to 13 and 10, and these are shown to be control characters.

The final statement shows the use of the char.IsControl method that receives two arguments, a string and index argument. It uses the same implementation but accesses the character in the string internally instead.

Internals. Let's peek inside the internals of the char.IsControl method in the .NET Framework using IL Disassembler. The IsControl method behaves differently depending on whether the character is a Unicode character or not.

And: If the character is not a Unicode character, a byte[] lookup table is used.

Then: The array is accessed by the value of the character as an index, and this is translated to true or false.

IL Disassembler Tutorial

How to examine this method. Compile this program in Visual Studio. Then open the EXE file in the IL Disassembler program. Then click on the IsControl method name to see the internals.

Summary. We examined the char.IsControl method in the C# language. This provides a way to test certain characters to see if they are "control" characters that provide behavior separate from just a letter or similar character.

Review: Control characters can indicate a beep or vertical space as well. This is only rarely a useful feature.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf