The Char.IsPunctuation()
method is a C# character method that is used to check if a character is categorized as a punctuation mark.
// for a single character
IsPunctuation(Char)
// for a character in a string
IsPunctuation(String, Int32)
Char
: The character we want to check.
String
: A string we want to check to see if its character specified at an index position is punctuation.
Int32
: The index position of a character in a string that we want to check to see if it is a punctuation mark or not.
Char.isPunctuation
returns a boolean value of true
if the character is punctuation; else, it returns false
.
We will demonstrate the use of the IsPunctuation()
method in the example below.
// use Systemusing System;// create classclass PunctuationChecker{// create methodstatic void Main(){// create characterschar c1 = '?';char c2 = '.';char c3 = ';';char c4 = 't';// create some stringsstring s1 = "Hello!";string s2 = "Edpresso";// check if characters are// punctuation marksbool a = Char.IsPunctuation(c1);bool b = Char.IsPunctuation(c2);bool c = Char.IsPunctuation(c3);bool d = Char.IsPunctuation(c4);bool e = Char.IsPunctuation(s1, 5); // last positionbool f = Char.IsPunctuation(s2, 2); // third position// print out returned valuesConsole.WriteLine(a); // TrueConsole.WriteLine(b); // TrueConsole.WriteLine(c); // TrueConsole.WriteLine(d); // FalseConsole.WriteLine(e); // TrueConsole.WriteLine(f); // False}}
As seen in the code above, every specified character is a punctuation mark except c4
, which is 't'
, and the third character in string s2
, which is 'r'
.