在 .NET 中更改大小写

如果你编写一个接受用户输入的应用程序,无法确保用户在输入数据时使用的大小写(大写或小写)会是什么。 您可能希望字符串大小写一致,尤其是在用户界面中显示它们时。 下表介绍了三种变更大小写的方法。 前两种方法提供接受文化信息的重载函数。

方法名称 Use
String.ToUpper 将字符串中的所有字符转换为大写。
String.ToLower 将字符串中的所有字符转换为小写。
TextInfo.ToTitleCase 将字符串转换为标题大小写。

警告

String.ToUpper不应使用和String.ToLower方法转换字符串,以便对其进行比较或测试它们是否相等。 有关详细信息,请参阅“比较混合大小写字符串”部分

比较混合大小写的字符串

若要比较混合大小写的字符串以确定其排序,请使用具有comparisonType参数的方法重载String.CompareTo之一,并为comparisonType参数提供StringComparison.CurrentCultureIgnoreCaseStringComparison.InvariantCultureIgnoreCaseStringComparison.OrdinalIgnoreCase中的一个值。 若要进行使用除了当前区域性之外的特定区域性的比较,请调用带有cultureoptions参数的String.CompareTo方法重载,并提供CompareOptions.IgnoreCase作为options参数的值。

若要比较混合大小写的字符串以确定它们是否相等,请调用方法 String.Equals 的一个重载,并使用 comparisonType 参数指定值 StringComparison.CurrentCultureIgnoreCaseStringComparison.InvariantCultureIgnoreCaseStringComparison.OrdinalIgnoreCase 作为 comparisonType 的参数。

有关详细信息,请参阅 有关使用字符串的最佳做法

ToUpper 方法

该方法 String.ToUpper 将字符串中的所有字符更改为大写。 以下示例将字符串“Hello World!”从混合大小写转换为大写。

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!

前面的示例默认是文化敏感的;它应用当前文化的文本大小写约定。 若要执行与区域性无关的大小写更改,或应用特定区域性的大小写规则,请使用String.ToUpper(CultureInfo)方法重载,并将表示指定区域性的CultureInfo.InvariantCulture值或System.Globalization.CultureInfo对象提供给culture参数。 要了解如何使用 ToUpper 方法执行不区分区域性的大小写更改,请参阅 执行不区分区域性的大小写更改

ToLower 方法

该方法 String.ToLower 类似于前面的方法,而是将字符串中的所有字符转换为小写。 以下示例将字符串“Hello World!”转换为小写。

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!

前面的示例在默认情况下对区域文化敏感;它应用当前文化的大小写惯例。 若要执行不区分文化的大小写转换或应用特定文化的大小写约定,请使用 String.ToLower(CultureInfo) 方法重载,并将表示指定文化的 CultureInfo.InvariantCulture 值或 System.Globalization.CultureInfo 对象传递给 culture 参数。 有关演示如何使用 ToLower(CultureInfo) 方法执行不区分区域性的大小写更改的示例,请参阅 执行不区分区域性的大小写更改

ToTitleCase 方法

TextInfo.ToTitleCase 每个单词的第一个字符转换为大写,其余字符转换为小写。 但是,完全大写的单词假定为首字母缩略词,不会转换。

该方法 TextInfo.ToTitleCase 对文化敏感; 即,它使用特定文化的字母大小写规则。 为了调用该方法,您首先要从特定区域性的 CultureInfo.TextInfo 属性中检索表示该区域性大小写约定的 TextInfo 对象。

以下示例将数组中的每个字符串传递给 TextInfo.ToTitleCase 该方法。 这些字符串包括适当的标题字符串以及首字母缩略词。 这些字符串通过使用美式英语的大小写约定转换为标题格式。

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($"{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

请注意,虽然它对文化很敏感,但 TextInfo.ToTitleCase 方法并不提供语言上正确的大小写规则。 例如,在前面的示例中,该方法将“两个城市的故事”转换为“两个城市的故事”。 然而,美式英语的语言规范的标题格式是“双城记”。

另请参阅