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.IsControl(String, Int32) Method in C#
The Char.IsControl(String, Int32) method in C# determines whether the character at a specified position in a string is a control character. Control characters are non-printable characters used for formatting and text control, such as tab (\t), newline (), and carriage return (\r).
Syntax
Following is the syntax for the Char.IsControl(String, Int32) method −
public static bool IsControl(string str, int index);
Parameters
-
str − The string to examine.
-
index − The zero-based position of the character to evaluate in the string.
Return Value
Returns true if the character at the specified index is a control character; otherwise, false.
Example with Regular Characters
Let us see an example with a string containing regular alphanumeric characters −
using System;
using System.Globalization;
public class Demo {
public static void Main() {
string val = "hjk9878hj";
Console.WriteLine("String = " + val);
UnicodeCategory unicode = Char.GetUnicodeCategory(val, 4);
Console.WriteLine("The value at specific index = " + unicode);
bool res = Char.IsControl(val, 4);
if (res)
Console.WriteLine("Control character found!");
else
Console.WriteLine("Control character isn't there");
}
}
The output of the above code is −
String = hjk9878hj The value at specific index = DecimalDigitNumber Control character isn't there
Example with Control Characters
Here is an example that demonstrates detecting actual control characters like tab and newline −
using System;
public class Demo {
public static void Main() {
string str = "Hello\tWorld<br>";
Console.WriteLine("String length: " + str.Length);
for (int i = 0; i < str.Length; i++) {
char ch = str[i];
bool isControl = Char.IsControl(str, i);
if (isControl) {
if (ch == '\t')
Console.WriteLine("Index " + i + ": Tab character (control)");
else if (ch == '<br>')
Console.WriteLine("Index " + i + ": Newline character (control)");
else
Console.WriteLine("Index " + i + ": Control character");
} else {
Console.WriteLine("Index " + i + ": '" + ch + "' (not control)");
}
}
}
}
The output of the above code is −
String length: 12 Index 0: 'H' (not control) Index 1: 'e' (not control) Index 2: 'l' (not control) Index 3: 'l' (not control) Index 4: 'o' (not control) Index 5: Tab character (control) Index 6: 'W' (not control) Index 7: 'o' (not control) Index 8: 'r' (not control) Index 9: 'l' (not control) Index 10: 'd' (not control) Index 11: Newline character (control)
Common Control Characters
Here are examples of common control characters that return true with IsControl −
using System;
public class ControlCharDemo {
public static void Main() {
string[] testStrings = {
"A\tB", // Tab character
"Line1\nLine2", // Newline
"Text\rReturn", // Carriage return
"Null\0End", // Null character
"Bell\aSound" // Bell character
};
foreach (string test in testStrings) {
Console.WriteLine("Testing: "" + test.Replace("\t", "\t").Replace("<br>", "\<br>").Replace("\r", "\r").Replace("\0", "\0").Replace("\a", "\a") + """);
for (int i = 0; i < test.Length; i++) {
if (Char.IsControl(test, i)) {
Console.WriteLine(" Control character at index " + i);
}
}
Console.WriteLine();
}
}
}
The output of the above code is −
Testing: "A\tB" Control character at index 1 Testing: "Line1\nLine2" Control character at index 5 Testing: "Text\rReturn" Control character at index 4 Testing: "Null\0End" Control character at index 4 Testing: "Bell\aSound" Control character at index 4
Conclusion
The Char.IsControl(String, Int32) method is useful for identifying non-printable control characters in strings at specific positions. It returns true for characters like tab, newline, carriage return, and other formatting control characters, making it valuable for text processing and validation scenarios.
