String.ToUpperInvariant 方法

定義

使用不因文化特性而異的大小寫規則,傳回轉換成大寫的這個 String 物件之複本。

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

傳回

目前字串的大寫對應項。

範例

下列範例會定義字串數位,其中包含多種語言的單一單字。 方法 ToUpperInvariant 可用來以不區分大小寫的每個字組版本填入平行陣列的專案。 方法 Array.Sort<TKey,TValue>(TKey[], TValue[], IComparer<TKey>) 是用來根據大寫陣列中的元素順序排序區分大小寫的陣列,以確保不論語言為何,項目都會以相同順序顯示。

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
//       Τρίτη
//       Вторник
//       יום שלישי
//       الثلاثاء
//       วันอังคาร
open System
open System.IO

do
    let words = 
        [| "Tuesday"; "Salı"; "Вторник"; "Mardi" 
           "Τρίτη"; "Martes"; "יום שלישי" 
           "الثلاثاء"; "วันอังคาร" |]
    use sw = new StreamWriter(@".\output.txt")
        
    // Display array in unsorted order.
    for word in words do
        sw.WriteLine word

    sw.WriteLine()

    // Create parallel array of words by calling ToUpperInvariant.
    let upperWords = words |> Array.map (fun x -> x.ToUpperInvariant())
    
    // Sort the words array based on the order of upperWords.
    Array.Sort(upperWords, words, StringComparer.InvariantCulture)
    
    // Display the sorted array.
    for word in words do
        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
'       Τρίτη
'       Вторник
'       יום שלישי
'       الثلاثاء
'       วันอังคาร

備註

不變異文化特性代表不區分文化特性的文化特性。 它與英文相關聯,但與特定國家或地區無關。 如需詳細資訊,請參閱 CultureInfo.InvariantCulture 屬性 (Property)。

如果您的應用程式取決於以不受目前文化特性影響之可預測方式變更的字串大小寫,請使用 ToUpperInvariant 方法。 方法 ToUpperInvariant 相當於 ToUpper(CultureInfo.InvariantCulture)。 當字串集合必須以使用者介面控件的可預測順序出現時,建議使用 方法。

注意

這個方法不會修改目前實例的值。 相反地,它會傳回新的字串,其中目前實例中的所有字元都會轉換成大寫。

如果您需要操作系統識別碼的小寫或大寫版本,例如檔名、命名管道或登錄機碼,請使用 ToLowerInvariantToUpperInvariant 方法。

適用於

另請參閱