Muokkaa

Jaa


Change case in .NET

If you write an application that accepts input from a user, you can never be sure what case (upper or lower) they will use to enter the data. Often, you want strings to be cased consistently, particularly if you are displaying them in the user interface. The following table describes three case-changing methods. The first two methods provide an overload that accepts a culture.

Method name Use
String.ToUpper Converts all characters in a string to uppercase.
String.ToLower Converts all characters in a string to lowercase.
TextInfo.ToTitleCase Converts a string to title case.

Warning

The String.ToUpper and String.ToLower methods should not be used to convert strings in order to compare them or test them for equality. For more information, see the Compare strings of mixed case section.

Compare strings of mixed case

To compare strings of mixed case to determine their ordering, call one of the overloads of the String.CompareTo method with a comparisonType parameter, and provide a value of either StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, or StringComparison.OrdinalIgnoreCase for the comparisonType argument. For a comparison using a specific culture other than the current culture, call an overload of the String.CompareTo method with both a culture and options parameter, and provide a value of CompareOptions.IgnoreCase as the options argument.

To compare strings of mixed case to determine whether they're equal, call one of the overloads of the String.Equals method with a comparisonType parameter, and provide a value of either StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, or StringComparison.OrdinalIgnoreCase for the comparisonType argument.

For more information, see Best practices for using strings.

ToUpper method

The String.ToUpper method changes all characters in a string to uppercase. The following example converts the string "Hello World!" from mixed case to uppercase.

string properString = "Hello World!";
Console.WriteLine(properString.ToUpper());
// This example displays the following output:
//       HELLO WORLD!
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.ToUpper())
' This example displays the following output:
'       HELLO WORLD!

The preceding example is culture-sensitive by default; it applies the casing conventions of the current culture. To perform a culture-insensitive case change or to apply the casing conventions of a particular culture, use the String.ToUpper(CultureInfo) method overload and supply a value of CultureInfo.InvariantCulture or a System.Globalization.CultureInfo object that represents the specified culture to the culture parameter. For an example that demonstrates how to use the ToUpper method to perform a culture-insensitive case change, see Perform culture-insensitive case changes.

ToLower method

The String.ToLower method is similar to the previous method, but instead converts all the characters in a string to lowercase. The following example converts the string "Hello World!" to lowercase.

string properString = "Hello World!";
Console.WriteLine(properString.ToLower());
// This example displays the following output:
//       hello world!
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.ToLower())
' This example displays the following output:
'       hello world!

The preceding example is culture-sensitive by default; it applies the casing conventions of the current culture. To perform a culture-insensitive case change or to apply the casing conventions of a particular culture, use the String.ToLower(CultureInfo) method overload and supply a value of CultureInfo.InvariantCulture or a System.Globalization.CultureInfo object that represents the specified culture to the culture parameter. For an example that demonstrates how to use the ToLower(CultureInfo) method to perform a culture-insensitive case change, see Perform culture-insensitive case changes.

ToTitleCase method

The TextInfo.ToTitleCase converts the first character of each word to uppercase and the remaining characters to lowercase. However, words that are entirely uppercase are assumed to be acronyms and are not converted.

The TextInfo.ToTitleCase method is culture-sensitive; that is, it uses the casing conventions of a particular culture. In order to call the method, you first retrieve the TextInfo object that represents the casing conventions of the particular culture from the CultureInfo.TextInfo property of a particular culture.

The following example passes each string in an array to the TextInfo.ToTitleCase method. The strings include proper title strings as well as acronyms. The strings are converted to title case by using the casing conventions of the English (United States) culture.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { "a tale of two cities", "gROWL to the rescue",
                          "inside the US government", "sports and MLB baseball",
                          "The Return of Sherlock Holmes", "UNICEF and children"};

      TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
      foreach (var value in values)
         Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value));
   }
}
// The example displays the following output:
//    a tale of two cities --> A Tale Of Two Cities
//    gROWL to the rescue --> Growl To The Rescue
//    inside the US government --> Inside The US Government
//    sports and MLB baseball --> Sports And MLB Baseball
//    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
//    UNICEF and children --> UNICEF And Children
Imports System.Globalization

Module Example
    Public Sub Main()
        Dim values() As String = {"a tale of two cities", "gROWL to the rescue",
                                   "inside the US government", "sports and MLB baseball",
                                   "The Return of Sherlock Holmes", "UNICEF and children"}

        Dim ti As TextInfo = CultureInfo.CurrentCulture.TextInfo
        For Each value In values
            Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value))
        Next
    End Sub
End Module
' The example displays the following output:
'    a tale of two cities --> A Tale Of Two Cities
'    gROWL to the rescue --> Growl To The Rescue
'    inside the US government --> Inside The US Government
'    sports and MLB baseball --> Sports And MLB Baseball
'    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
'    UNICEF and children --> UNICEF And Children

Note that although it is culture-sensitive, the TextInfo.ToTitleCase method does not provide linguistically correct casing rules. For instance, in the previous example, the method converts "a tale of two cities" to "A Tale Of Two Cities". However, the linguistically correct title casing for the en-US culture is "A Tale of Two Cities."

See also