String.ToLowerInvariant 方法
定義
public:
System::String ^ ToLowerInvariant();
public string ToLowerInvariant ();
member this.ToLowerInvariant : unit -> string
Public Function ToLowerInvariant () As String
傳回
目前字串的小寫對應項。The lowercase equivalent of the current string.
範例
下列範例會定義字串陣列,其中包含多種語言的單一單字。The following example defines a string array that contains a single word in a number of languages. ToLowerInvariant方法是用來填入平行陣列的元素,其中包含每個單字的不區分大小寫版本。The ToLowerInvariant 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 lowercase array to ensure that elements appear in the same order regardless of language.
using System;
public class Example
{
public static void Main()
{
string[] words = { "Tuesday", "Salı", "Вторник", "Mardi",
"Τρίτη", "Martes", "יום שלישי",
"الثلاثاء", "วันอังคาร" };
// Display array in unsorted order.
foreach (string word in words)
Console.WriteLine(word);
Console.WriteLine();
// Create parallel array of words by calling ToLowerInvariant.
string[] lowerWords = new string[words.Length];
for (int ctr = words.GetLowerBound(0); ctr <= words.GetUpperBound(0); ctr++)
lowerWords[ctr] = words[ctr].ToLowerInvariant();
// Sort the words array based on the order of lowerWords.
Array.Sort(lowerWords, words, StringComparer.InvariantCulture);
// Display the sorted array.
foreach (string word in words)
Console.WriteLine(word);
}
}
// The example displays the following output:
// Tuesday
// Salı
// Вторник
// Mardi
// Τρίτη
// Martes
// יום שלישי
// الثلاثاء
// วันอังคาร
//
// Mardi
// Martes
// Salı
// Tuesday
// Τρίτη
// Вторник
// יום שלישי
// الثلاثاء
// วันอังคาร
Module Example
Public Sub Main()
Dim words() As String = { "Tuesday", "Salı", "Вторник", "Mardi", _
"Τρίτη", "Martes", "יום שלישי", _
"الثلاثاء", "วันอังคาร" }
' Display array in unsorted order.
For Each word As String In words
Console.WriteLine(word)
Next
Console.WriteLine()
' Create parallel array of words by calling ToLowerInvariant.
Dim lowerWords(words.Length - 1) As String
For ctr As Integer = words.GetLowerBound(0) To words.GetUpperBound(0)
lowerWords(ctr) = words(ctr).ToLowerInvariant()
Next
' Sort the words array based on the order of lowerWords.
Array.Sort(lowerWords, words, StringComparer.InvariantCulture)
' Display the sorted array.
For Each word As String In words
Console.WriteLine(word)
Next
End Sub
End Module
' The example displays 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.
如果您的應用程式相依于字串以可預測的方式變更,但不受目前文化特性影響的案例,請使用 ToLowerInvariant 方法。If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToLowerInvariant method. ToLowerInvariant方法相當於 ToLower(CultureInfo.InvariantCulture)
。The ToLowerInvariant method is equivalent to ToLower(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 lowercase.
安全性考量Security Considerations
如果您需要作業系統識別碼的小寫或大寫版本(例如檔案名、具名管道或登錄機碼),請使用 ToLowerInvariant 或 ToUpperInvariant 方法。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.