
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# String - ToUpper() Method
The C# String ToUpper() method converts all character in a string to upper case and is used to perform case-insensitive operations, normalize strings for comparison, or handle user input.
Syntax
Following are the syntax of the C# string ToUpper() method −
Default Syntax −
This syntax return copy of the current string converted to lower case.
public string ToUpper();
Parametrised Syntax −
This syntax returns a copy of this string converted to uppercase, using the casing rules of the specified culture.
public string ToUpper(System.Globalization.CultureInfo? culture);
Parameters
This method accepts the following parameters −
- culture: This is an object that supplies culture-specific wrapper rules. If culture is null, the current culture is used.
Return Value
This method returns a string that is in uppercase equivalent to the current string.
Example 1: Using ToUpper with Current Culture
Following is a basic example of the ToUpper() method to convert the string into uppercase with the current culture −
using System; class Program { public static void Main() { string str = "Hello, tpians!"; string ToUpper = str.ToUpper(); Console.Write("Converted String: "+ ToUpper); } }
Output
Following is the output −
Converted String: HELLO, TPIANS!
Example 2: Using ToUpper(CultureInfo) with a specific culture
Let's look at another example. Here, we use the parametrised ToUpper() method to convert a string into uppercase with specific culture −
using System; using System.Globalization; class Program { static void Main() { // Turkish character '' string str = "stanbul"; //use InvariantCulture string lowerInvariant = str.ToUpper(CultureInfo.InvariantCulture); string lowerTurkish = str.ToUpper(new CultureInfo("tr-TR")); Console.WriteLine(lowerInvariant); Console.WriteLine(lowerTurkish); } }
Output
Following is the output −
STANBUL STANBUL
Example 3: Case-Insensitive Comparisons
In this example, we convert the string into uppercase using the ToUpper() method. We then compare string −
using System; class Program { public static void Main() { string userInput = "Admin"; if (userInput.ToUpper() == "ADMIN") { Console.WriteLine("Access granted!"); } } }
Output
Following is the output −
Access granted!