find and solve || findandsolve.com
Please wait.....

C# String ToUpper()

In the C#,the ToUpper method is often used to convert a string to uppercase so that it can be used in a case-insensitive comparison or uses to uppper case using c# programming language.The String.ToUpper()  methods in C# and .NET convert a string into an uppercase  string respectively. These methods are easy to use in c#.

String.ToUpper() Method

String.ToUpper() method is used to returns a copy of the current string converted to uppercase.

Syntax
String.ToUpper()

Return Type: Its return the string value, which is the uppercase equivalent of the given string.

public class Program
{
 public static void Main()
 {
      String str = "Find And Solve";
        String result = str.ToUpper();
        Console.WriteLine("Original String is : {0}" ,str);
        Console.WriteLine("Result of ToUpper(): {0}",result);
 }

Output

Original String is : Find And Solve

Result of ToUpper(): FIND AND SOLVE


String.ToUpper(CultureInfo) Method

This method is used to return a copy of the current string converted to uppercase, using the casing rules of the specified culture.

Syntax

public string ToUpper (System.Globalization.CultureInfo culture);

Example 

using System;
using System.Globalization;
public class Program {
    // Main Method
    public static void Main()
    {
        // original string
        string str = "Find And Solve";
        // string converted to Uppercase by
        // using English-United States culture
        string upperstr = str.ToUpper(new CultureInfo("en-US", false));
        Console.WriteLine(upperstr);
    }
}

Ouutput

FIND AND SOLVE

ToUpper() – Null String

In given below example, you will take a null string and call ToUpper() method on this null string. ToUpper() should throw System.NullReferenceException

using System;
public class Example {
    static void Main(string[] args) {
        String strNull = null;
        String result = strNull .ToUpper();
        Console.WriteLine("Original String is :",strNull );
        Console.WriteLine("Result of ToUpper() :",strNull );
    }
}

Output

Run-time exception (line 11): Object reference not set to an instance of an object.

Stack Trace:

[System.NullReferenceException: Object reference not set to an instance of an object.]

   at Program.Main() :line 11


Convert first letter of a string to uppercase

using System;
public class Program {
    // Main Method
    public static void Main()
    {
        // convert first letter of a string uppercase
string name = "find and solve";
if (!string.IsNullOrEmpty(name))
{
    name = char.ToUpper(name[0]) + name.Substring(1);
}
Console.WriteLine("String with first letter uppercase: {0}",name);
    }
}

Output

String with first letter uppercase: Find and solve

Conclusion

In this C# Tutorial, we have learnt the syntax of C# String.ToUpper() method, and also learnt how to use this method with the help of C# example programs.