Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

C# Fundamental Training : DAY 5

Prepared By Nagma Khan


Char >> belongs to int family
• It’s used to store single character.
• It can store alphabet, numeric & special characters.
• It’s a value type. Its default value will be ZERO.
• Its initialize within single quotes.
• Every single character has an underlying integer value. eg. c=99
• Example: Console.WriteLine((int)c);
• If we perfom concatination on char, then it will return addition of integral values if the char.
• Example: char ch1=’c’; char ch2=’d’; int ch1+ch2;
• C# allows to work with ASCII character set and UTF-18 character set
Methods & Constant of char

• IsLetterOrDigit() • MinValue
• IsWhiteSpace() • MaxValue
• IsDigit()
• IsLetter()
• IsUpper()
• IsLower()
• ToUpper()
• ToLower()
Constant of char

MaxValue: It is a constant field which represents the largest possible value of a Char.
Example:
Console.WriteLine(char.MaxValue);
MinValue: It is a constant field which represents the smallest possible value of a Char.
Example:
Console.WriteLine(char.MinValue);
Methods of char

IsLetterOrDigit()
Indicates whether a Unicode character is categorized as a letter or a decimal digit.
Syntax:
public static bool IsLetterOrDigit(char ch);
Parameter:
ch: It is required Unicode character of System.char type which is to be checked.
Return Type: The method returns True, if it successfully matches any Unicode letter or decimal digit, otherwise
returns False. The return type of this method is System.Boolean.
Example:
bool result; // checking if G is a // letter or decimal digit char ch1 = 'G'; result = Char.IsLetterOrDigit(ch1);
Console.WriteLine(result); // checking if '@' is a // letter or decimal digitchar ch2 = '@';
result = Char.IsLetterOrDigit(ch2); Console.WriteLine(result);
Methods of char

IsWhiteSpace()
Indicates whether a Unicode character is categorized as white space.
Syntax: public static bool IsWhiteSpace(char ch);
Parameter:
ch: It is required Unicode character of System.char type which is to be checked for whitespace.
Return Type : The method returns True if the specified Unicode character is a whitespace character, otherwise
returns False. The return type of this method is System.Boolean.
Example: // checking if space is a whitespace
bool result; char ch1 = ' '; result = Char.IsWhiteSpace(ch1); Console.WriteLine(result);
// checking if hyphen is a whitespace
char ch3 = '-';
result = Char.IsWhiteSpace(ch3);
Console.WriteLine(result);
Methods of char

IsDigit()
Indicates whether a Unicode character is categorized as a decimal digit.
Syntax: public static bool IsDigit(char ch);
Parameter:
ch: It is required Unicode character of System.char type which is to be checked.
Return Type: The method returns True, if it successfully matches any decimal digit, otherwise returns False. The
return type of this method is System.Boolean.
Example: bool result;
// checking if 5 is a digit or not
char ch1 = '5'; result = Char.IsDigit(ch1); Console.WriteLine(result);
// checking if 'c' is a digit
char ch2 = 'c'; result = Char.IsDigit(ch2);
Console.WriteLine(result);
Methods of char

IsLetter()
Indicates whether a Unicode character is categorized as a Unicode letter.
Syntax: public static bool IsLetter(char ch);
Parameter:
ch: It is required Unicode character of System.char type which is to be checked.
Return Type: The method returns True, if it successfully matches any Unicode letter, otherwise returns False. The
return type of this method is System.Boolean.
Example: bool result;
// checking if G is a Unicode letter or not
char ch1 = 'G'; result = Char.IsLetter(ch1); Console.WriteLine(result);
// checking if '6' is a Unicode letter or not
char ch2 = '6';
result = Char.IsLetter(ch2);
Console.WriteLine(result);
Methods of char

IsUpper()
Indicates whether a Unicode character is categorized as an uppercase letter.
Syntax: public static bool IsUpper(char ch);
Parameter:
ch: It is required Unicode character of System.char type which is to be checked.
Return Type: The method returns True, if it successfully matches any uppercase letter, otherwise returns False.
The return type of this method is System.Boolean.
Example:bool result;
// checking if G is a uppercase letter or not
char ch1 = 'G'; result = Char.IsUpper(ch1);Console.WriteLine(result);
// checking if 'g' is a uppercase letter or not
char ch2 = 'g';
result = Char.IsUpper(ch2);
Console.WriteLine(result);
Methods of char

IsLower()
Indicates whether a Unicode character is categorized as a lowercase letter.
Syntax: public static bool IsLower(char ch);
Parameter:
ch: It is required Unicode character of System.char type which is to be checked.
Return Type: The method returns True, if it successfully matches any lowercase letter, otherwise returns False.
The return type of this method is System.Boolean.
Example: bool result;
// checking if G is a lowercase letter or not
char ch1 = 'G'; result = Char.IsLower(ch1);Console.WriteLine(result);
// checking if 'g' is a lowercase letter or not
char ch2 = 'g';
result = Char.IsLower(ch2);
Methods of char

ToUpper()
Converts the value of a Unicode character to its uppercase equivalent.
Example:
char ch = 'd';
char ch1 = char.ToUpper(ch);
ToLower()
Converts the value of a Unicode character to its lowercase equivalent.
char ch = 'D';
char ch1 = char.ToLower(ch);
PROGRAMS TO PRACTICE

• Write C# program to print ASCII values of all characters.


• Practice All Methods and Properties of Char, discussed in this session.
• Write a program in C# Sharp to create a function to input a string and count
number of spaces are in the string.
Test Data :
Please input a string : This is a test string.
Expected Output :"This is a test string." contains 4 spaces
Best Of Luck
Thank You!!!

You might also like