为了对
   
    String.ToUpper
   
   和
   
    String.ToLower
   
   方法进行比较或测试它们是否相等,这两种方法不应用于转换字符串。 有关详细信息,请参阅
   
    比较混合大小写的字符串
   
   部分。
  
  
   比较混合大小写的字符串
  
  
   若要比较混合大小写的字符串以确定它们的顺序,请调用具有
   
    String.CompareTo
   
   方法中具有
   
    comparisonType
   
   参数的其中一个重载,并向
   
    comparisonType
   
   自变量提供
   
    StringComparison.CurrentCultureIgnoreCase
   
   、
   
    StringComparison.InvariantCultureIgnoreCase
   
   或
   
    StringComparison.OrdinalIgnoreCase
   
   的值。 对于使用特定区域性(而非当前区域性)的比较,请调用
   
    String.CompareTo
   
   方法中具有
   
    culture
   
   和
   
    options
   
   参数的重载,并提供
   
    CompareOptions.IgnoreCase
   
   的值作为
   
    options
   
   参数。
  
  
   若要比较混合大小写的字符串以确定它们是否相等,请调用
   
    String.Equals
   
   方法中具有
   
    comparisonType
   
   参数的其中一个重载,并向
   
    comparisonType
   
   自变量提供
   
    StringComparison.CurrentCultureIgnoreCase
   
   、
   
    StringComparison.InvariantCultureIgnoreCase
   
   或
   
    StringComparison.OrdinalIgnoreCase
   
   的值。
  
  
   有关详细信息,请参阅
   
    有关使用字符串的最佳做法
   
   。
  
  
   
    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) 方法重载,并向 culture 参数提供 CultureInfo.InvariantCulture 值或表示指定区域性的 System.Globalization.CultureInfo 对象。 有关展示如何使用 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) 方法重载,并向 culture 参数提供 CultureInfo.InvariantCulture 值或表示指定区域性的 System.Globalization.CultureInfo 对象。 有关展示如何使用 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("{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))
    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 方法虽然区分区域性,但不提供语言方面的正确大小写规则。 例如,在上述示例中,方法将“a tale of two cities”转换为“A Tale Of Two Cities”。 但是,对于 en-US 区域性,语言方面的正确首字母大小写应为“A Tale of Two Cities”。
基本字符串操作
执行不区分区域性的字符串操作