Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
character
uppercase
communitycreator

How to check if a character is uppercase in C#

Theodore Kelechukwu Onyejiaku

Overview

In C#, you can use the IsUpper() method see if a character is uppercase. This method indicates whether or not a certain character is uppercase.

Also, in a string, we can check if any of its characters is uppercase by specifying the index position of the character.

Syntax

// for a character
IsUpper(Char)
// for a string
IsUpper(String, Int32)

Parameters

Char: this is the character we want to check to see if it is uppercase.

String: this is a string that we want to check to see if any of its characters are uppercase.

Int32: this is the index position of the character in a string that we want to check to see if it is uppercase.

Return Value

The value returned is a Boolean. true is returned if the character is uppercase, else false is returned.

Coding example

In the coding example below, we create some characters and some strings and then check if some of the characters are uppercase using the IsUpper() method.

// use System
using System;
// create class
class UppercaseChecker
{
    // main method
    static void Main()
    {
      // create characters
      char c1 = 'H';
      char c2 = 'i';
      char c3 = 'E';

      // create strings
      string s1 = "Hi!";
      string s2 = "Edpresso";

      // check for uppercase
      bool a = Char.IsUpper(c1);
      bool b = Char.IsUpper(c2);
      bool c = Char.IsUpper(c3);
      bool d = Char.IsUpper(s1, 0); // first character
      bool e = Char.IsUpper(s2, 1); // second character

      // print returned values
      Console.WriteLine(a);  // True
      Console.WriteLine(b);  // False
      Console.WriteLine(c);  // True
      Console.WriteLine(d);  // True
      Console.WriteLine(e);  // False
    }
}
Check uppercase characters in C# using IsUpper() method

Explanation

  • In line 10, line 11, and line 12 we created characters, c1, c2, and c3.

  • In line 15 and line 16, we created strings s1 and s2.

  • In line 19 to 21 we checked if the characters created were uppercase using the IsUpper() method.

  • In line 22 and line 23, we checked if some characters in the strings we created were uppercase using the IsUpper() method. We did this by passing the index positions of the characters.

  • Finally, we printed the results from line 26 to line 30.

When the code runs, we see that all the characters specified were uppercase characters except character c2 and the second character of string s2, which returned false.

RELATED TAGS

c#
character
uppercase
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring