Export (0) Print
Expand All

String.ToUpper Method (CultureInfo)

Returns a copy of this String converted to uppercase, using the casing rules of the specified culture.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string ToUpper (
	CultureInfo culture
)
public String ToUpper (
	CultureInfo culture
)
public function ToUpper (
	culture : CultureInfo
) : String
Not applicable.

Parameters

culture

A CultureInfo object that supplies culture-specific casing rules.

Return Value

A String in uppercase.

Exception typeCondition

ArgumentNullException

culture is a null reference (Nothing in Visual Basic).

The casing rules of the culture that is currently in effect determines the way the case of a string is changed. If your application depends on the case of a string changing in a predictable way, and the effective culture changes unexpectedly, then you can get invalid results. Consider the following lookup routine where the lookup key is a text string, the key is forced to a standard case to simplify comparisons, and the effective culture is the culture associated with the current thread.

static object LookupKey(string key) { return internalHashtable[key.ToUpper()]; }

If it is possible for the current thread and its associated culture to change unexpectedly, it is advisable to specify the invariant culture instead of defaulting to the current thread culture. The following example demonstrates the preferred technique.

static object LookupKey(string key) { return internalHashtable[key.ToUpper(CultureInfo.InvariantCulture)]; }

The following code example converts a string of lowercase characters to two strings of uppercase characters using the English-United States and Turkish-Turkey cultures, then compares the uppercase strings. The uppercase strings are identical except that for each occurrence of the Unicode LATIN CAPITAL LETTER I in one string, the other string contains LATIN CAPITAL LETTER I WITH DOT ABOVE.

// Sample for String.ToUpper(CultureInfo)
using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    String str1 = "indigo";
    String str2, str3;

    Console.WriteLine();
    Console.WriteLine("str1 = '{0}'", str1);

    Console.WriteLine("str2 = Upper case copy of str1 using English-United States culture.");
    // str2 is an upper case copy of str1, using English-United States culture.
    str2 = str1.ToUpper(new CultureInfo("en-US", false));

    // str3 is an upper case copy of str1, using Turkish-Turkey culture.
    Console.WriteLine("str3 = Upper case copy of str1 using Turkish-Turkey culture.");
    str3 = str1.ToUpper(new CultureInfo("tr-TR", false));

    // Compare the code points in str2 and str3.
    Console.WriteLine();
    Console.WriteLine("str2 is {0} to str3.", 
         ((0 == String.CompareOrdinal(str2, str3)) ? "equal" : "not equal"));
    CodePoints("str1", str1);
    CodePoints("str2", str2);
    CodePoints("str3", str3);
    }

    public static void CodePoints(String title, String s)
    {
    Console.Write("{0}The code points in {1} are: {0}", Environment.NewLine, title);
    foreach (ushort u in s)
      Console.Write("{0:x4} ", u);
    Console.WriteLine();
    }
}
/*
This example produces the following results:

str1 = 'indigo'
str2 = Upper case copy of str1 using English-United States culture.
str3 = Upper case copy of str1 using Turkish-Turkey culture.

str2 is not equal to str3.

The code points in str1 are:
0069 006e 0064 0069 0067 006f

The code points in str2 are:
0049 004e 0044 0049 0047 004f

The code points in str3 are:
0130 004e 0044 0130 0047 004f
*/

// Sample for String.ToUpper(CultureInfo)
import System.*;
import System.Globalization.*;

class Sample
{
    public static void main(String[] args)
    {
        String str1 = "indigo";
        String str2, str3;

        Console.WriteLine();
        Console.WriteLine("str1 = '{0}'", str1);

        Console.WriteLine("str2 = Upper case copy of str1 using English-"
            + "United States culture.");

        // str2 is an upper case copy of str1, using English-United States 
        // culture.
        str2 = str1.ToUpper(new CultureInfo("en-US", false));

        // str3 is an upper case copy of str1, using Turkish-Turkey culture.
        Console.WriteLine("str3 = Upper case copy of str1 using Turkish-"
            + "Turkey culture.");
        str3 = str1.ToUpper(new CultureInfo("tr-TR", false));

        // Compare the code points in str2 and str3.
        Console.WriteLine();
        Console.WriteLine("str2 is {0} to str3.", 
            (0 == String.CompareOrdinal(str2, str3)) ? "equal" : "not equal");
        CodePoints("str1", str1);
        CodePoints("str2", str2);
        CodePoints("str3", str3);
    } //main

    public static void CodePoints(String title, String s)
    {
        Console.Write("{0}The code points in {1} are: {0}", 
            Environment.get_NewLine(), title);
        for (int iCtr = 0; iCtr < s.get_Length(); iCtr++) {
            UInt16 u = (UInt16)s.charAt(iCtr);
            Console.Write("{0:x4} ", u);
        }
        Console.WriteLine();
    } //CodePoints
} //Sample
/*
This example produces the following results:

str1 = 'indigo'
str2 = Upper case copy of str1 using English-United States culture.
str3 = Upper case copy of str1 using Turkish-Turkey culture.

str2 is not equal to str3.

The code points in str1 are:
0069 006e 0064 0069 0067 006f

The code points in str2 are:
0049 004e 0044 0049 0047 004f

The code points in str3 are:
0130 004e 0044 0130 0047 004f
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft