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.IsPunctuation() Method in C#
The Char.IsPunctuation() method in C# determines whether a specified Unicode character is categorized as a punctuation mark. This method is useful for text processing, input validation, and character analysis tasks.
Syntax
Following is the syntax −
public static bool IsPunctuation(char ch);
Parameters
-
ch − The Unicode character to evaluate.
Return Value
Returns true if the character is a punctuation mark; otherwise, false.
Using Char.IsPunctuation() with Different Characters
Example with Non-Punctuation Character
using System;
public class Demo {
public static void Main() {
bool res;
char val = 'q';
Console.WriteLine("Value = " + val);
res = Char.IsPunctuation(val);
Console.WriteLine("Is the value a punctuation? = " + res);
}
}
The output of the above code is −
Value = q Is the value a punctuation? = False
Example with Punctuation Character
using System;
public class Demo {
public static void Main() {
bool res;
char val = ',';
Console.WriteLine("Value = " + val);
res = Char.IsPunctuation(val);
Console.WriteLine("Is the value a punctuation? = " + res);
}
}
The output of the above code is −
Value = , Is the value a punctuation? = True
Testing Multiple Punctuation Characters
Example
using System;
public class Demo {
public static void Main() {
char[] chars = { '!', '?', '.', 'A', '5', '(', '}', '#', '@' };
foreach (char ch in chars) {
Console.WriteLine($"'{ch}' is punctuation: {Char.IsPunctuation(ch)}");
}
}
}
The output of the above code is −
'!' is punctuation: True
'?' is punctuation: True
'.' is punctuation: True
'A' is punctuation: False
'5' is punctuation: False
'(' is punctuation: True
'}' is punctuation: True
'#' is punctuation: True
'@' is punctuation: True
Common Use Cases
-
Text parsing − Identifying punctuation marks when processing sentences or documents.
-
Input validation − Checking if user input contains only specific character types.
-
String cleaning − Removing or replacing punctuation marks from text data.
Conclusion
The Char.IsPunctuation() method provides an efficient way to identify punctuation characters in Unicode text. It returns true for standard punctuation marks like periods, commas, and brackets, making it valuable for text processing and validation tasks.
