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 屬性 (Property)。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.

適用於

另請參閱