String.ToUpperInvariant 方法

定义

返回此 String 对象的转换为大写形式的副本,返回时使用固定区域性的大小写规则。Returns a copy of this String object converted to uppercase using the casing rules of the invariant culture.

public:
 System::String ^ ToUpperInvariant();
public string ToUpperInvariant ();
member this.ToUpperInvariant : unit -> string
Public Function ToUpperInvariant () As String

返回

当前字符串的大写形式。The uppercase equivalent of the current string.

示例

下面的示例定义了一个字符串数组,其中包含多种语言的单个单词。The following example defines a string array that contains a single word in a number of languages. ToUpperInvariant 方法用于使用每个单词的不区分大小写的版本填充并行数组的元素。The ToUpperInvariant method is used to populate the elements of a parallel array with the case-insensitive version of each word. Array.Sort<TKey,TValue>(TKey[], TValue[], IComparer<TKey>) 方法用于根据大写数组中元素的顺序对区分大小写的数组进行排序,以确保元素按与语言无关的顺序显示。The Array.Sort<TKey,TValue>(TKey[], TValue[], IComparer<TKey>) method is used to sort the case-sensitive array based on the order of elements in the uppercase array to ensure that elements appear in the same order regardless of language.

using System;
using System.IO;

public class Example
{
   public static void Main()
   {
      string[] words = { "Tuesday", "Salı", "Вторник", "Mardi", 
                         "Τρίτη", "Martes", "יום שלישי", 
                         "الثلاثاء", "วันอังคาร" };
      StreamWriter sw = new StreamWriter(@".\output.txt");
            
      // Display array in unsorted order.
      foreach (string word in words)
         sw.WriteLine(word);

      sw.WriteLine();

      // Create parallel array of words by calling ToUpperInvariant.
      string[] upperWords = new string[words.Length];
      for (int ctr = words.GetLowerBound(0); ctr <= words.GetUpperBound(0); ctr++)
         upperWords[ctr] = words[ctr].ToUpperInvariant();
      
      // Sort the words array based on the order of upperWords.
      Array.Sort(upperWords, words, StringComparer.InvariantCulture);
      
      // Display the sorted array.
      foreach (string word in words)
         sw.WriteLine(word);

      sw.Close();      
   }
}
// The example produces the following output:
//       Tuesday
//       Salı
//       Вторник
//       Mardi
//       Τρίτη
//       Martes
//       יום שלישי
//       الثلاثاء
//       วันอังคาร
//       
//       Mardi
//       Martes
//       Salı
//       Tuesday
//       Τρίτη
//       Вторник
//       יום שלישי
//       الثلاثاء
//       วันอังคาร
Imports System.IO

Module Example
   Public Sub Main()
      Dim words() As String = { "Tuesday", "Salı", "Вторник", "Mardi", _
                                "Τρίτη", "Martes", "יום שלישי", _
                                "الثلاثاء", "วันอังคาร" }
      Dim sw As New StreamWriter(".\output.txt")
      
      ' Display array in unsorted order.
      For Each word As String In words
         sw.WriteLine(word)
      Next
      sw.WriteLine()

      ' Create parallel array of words by calling ToUpperInvariant.
      Dim upperWords(words.Length - 1) As String
      For ctr As Integer = words.GetLowerBound(0) To words.GetUpperBound(0)
         upperWords(ctr) = words(ctr).ToUpperInvariant()
      Next
      
      ' Sort the words array based on the order of upperWords.
      Array.Sort(upperWords, words, StringComparer.InvariantCulture)
      
      ' Display the sorted array.
      For Each word As String In words
         sw.WriteLine(word)
      Next
      
      sw.Close()
   End Sub
End Module
' The example produces the following output:
'       Tuesday
'       Salı
'       Вторник
'       Mardi
'       Τρίτη
'       Martes
'       יום שלישי
'       الثلاثاء
'       วันอังคาร
'       
'       Mardi
'       Martes
'       Salı
'       Tuesday
'       Τρίτη
'       Вторник
'       יום שלישי
'       الثلاثاء
'       วันอังคาร

注解

固定区域性表示不区分区域性的区域性。The invariant culture represents a culture that is culture-insensitive. 它与英语相关联,而不是与特定国家或地区相关联。It is associated with the English language but not with a specific country or region. 有关更多信息,请参见 CultureInfo.InvariantCulture 属性。For more information, see the CultureInfo.InvariantCulture property.

如果你的应用程序依赖于以可预测方式(不受当前区域性影响)更改的字符串,请使用 ToUpperInvariant 方法。If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToUpperInvariant method. ToUpperInvariant 方法等效于 ToUpper(CultureInfo.InvariantCulture)The ToUpperInvariant method is equivalent to ToUpper(CultureInfo.InvariantCulture). 如果在用户界面控件中必须按可预测顺序显示字符串集合,则建议使用方法。The method is recommended when a collection of strings must appear in a predictable order in a user interface control.

备注

此方法不会修改当前实例的值。This method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中当前实例中的所有字符都转换为大写形式。Instead, it returns a new string in which all characters in the current instance are converted to uppercase.

安全注意事项Security Considerations

如果需要操作系统标识符的小写或大写版本(如文件名、命名管道或注册表项),请使用 ToLowerInvariantToUpperInvariant 方法。If you need the lowercase or uppercase version of an operating system identifier, such as a file name, named pipe, or registry key, use the ToLowerInvariant or ToUpperInvariant methods.

适用于

另请参阅