String.ToLower 方法

定义

返回此字符串转换为小写形式的副本。

重载

ToLower()

返回此字符串转换为小写形式的副本。

ToLower(CultureInfo)

根据指定区域性的大小写规则返回此字符串转换为小写形式的副本。

ToLower()

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

返回此字符串转换为小写形式的副本。

C#
public string ToLower();

返回

一个小写字符串。

示例

以下示例将多个混合大小写字符串转换为小写。

C#
using System;

public class ToLowerTest {
    public static void Main() {

        string [] info = {"Name", "Title", "Age", "Location", "Gender"};

        Console.WriteLine("The initial values in the array are:");
        foreach (string s in info)
            Console.WriteLine(s);

        Console.WriteLine("{0}The lowercase of these values is:", Environment.NewLine);

        foreach (string s in info)
            Console.WriteLine(s.ToLower());

        Console.WriteLine("{0}The uppercase of these values is:", Environment.NewLine);

        foreach (string s in info)
            Console.WriteLine(s.ToUpper());
    }
}
// The example displays the following output:
//       The initial values in the array are:
//       Name
//       Title
//       Age
//       Location
//       Gender
//
//       The lowercase of these values is:
//       name
//       title
//       age
//       location
//       gender
//
//       The uppercase of these values is:
//       NAME
//       TITLE
//       AGE
//       LOCATION
//       GENDER

注解

此方法考虑了当前区域性的大小写规则。

备注

此方法不会修改当前实例的值。 相反,它返回一个新字符串,其中当前实例中的所有字符都转换为小写。

调用 ToLower() 方法产生的大小写操作考虑了当前区域性的大小写约定。 如果需要操作系统标识符的小写或大写版本(如文件名、命名管道或注册表项),请使用 ToLowerInvariantToUpperInvariant 方法。 这与方法) 不同, ToLower() 这在每个区域性 (产生相同的结果,并且执行效率更高。

调用方说明

使用字符串的最佳做法中所述,建议避免调用替换默认值的字符串大小写方法,而是调用需要显式指定参数的方法。 若要使用当前区域性的大小写约定将字符转换为小写,请通过对其参数的 值为 CurrentCultureculture 调用方法重载来显式发出ToLower(CultureInfo)信号。 如果不需要语言感知比较,请考虑使用 Ordinal

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToLower(CultureInfo)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

根据指定区域性的大小写规则返回此字符串转换为小写形式的副本。

C#
public string ToLower(System.Globalization.CultureInfo? culture);
C#
public string ToLower(System.Globalization.CultureInfo culture);

参数

culture
CultureInfo

一个对象,用于提供区域性特定的大小写规则。 如果 culturenull,则使用当前区域性。

返回

当前字符串的等效小写形式。

示例

下面的示例使用 English-United 状态和 Turkish-Turkey 区域性将两个大写字符的字符串转换为小写字符,然后比较小写字符串。 大写字符串是相同的,只不过对于一个字符串中每出现 Unicode LATIN 大写字母 I,另一个字符串包含带上点的拉丁文大写字母 I。

C#
// Sample for String.ToLower(CultureInfo)

using System;
using System.Globalization;

class Sample
{
    public static void Main()
    {
    String str1 = "INDIGO";
    // str2 = str1, except each 'I' is '\u0130' (Unicode LATIN CAPITAL I WITH DOT ABOVE).
    String str2 = new String(new Char[] {'\u0130', 'N', 'D', '\u0130', 'G', 'O'});
    String str3, str4;

    Console.WriteLine();
    Console.WriteLine("str1 = '{0}'", str1);

    Console.WriteLine();
    Console.WriteLine("str1 is {0} to str2.",
         ((0 == String.CompareOrdinal(str1, str2)) ? "equal" : "not equal"));
    CodePoints("str1", str1);
    CodePoints("str2", str2);

    Console.WriteLine();
    // str3 is a lower case copy of str2, using English-United States culture.
    Console.WriteLine("str3 = Lower case copy of str2 using English-United States culture.");
    str3 = str2.ToLower(new CultureInfo("en-US", false));

    // str4 is a lower case copy of str2, using Turkish-Turkey culture.
    Console.WriteLine("str4 = Lower case copy of str2 using Turkish-Turkey culture.");
    str4 = str2.ToLower(new CultureInfo("tr-TR", false));

    // Compare the code points in str3 and str4.
    Console.WriteLine();
    Console.WriteLine("str3 is {0} to str4.",
         ((0 == String.CompareOrdinal(str3, str4)) ? "equal" : "not equal"));
    CodePoints("str3", str3);
    CodePoints("str4", str4);
    }

    public static void CodePoints(String title, String s)
    {
    Console.Write("{0}The code points in {1} are: {0}", Environment.NewLine, title);
    foreach (ushort u in s)
      Console.Write("{0:x4} ", u);
    Console.WriteLine();
    }
}
/*
This example produces the following results:

str1 = 'INDIGO'

str1 is not equal to str2.

The code points in str1 are:
0049 004e 0044 0049 0047 004f

The code points in str2 are:
0130 004e 0044 0130 0047 004f

str3 = Lower case copy of str2 using English-United States culture.
str4 = Lower case copy of str2 using Turkish-Turkey culture.

str3 is equal to str4.

The code points in str3 are:
0069 006e 0064 0069 0067 006f

The code points in str4 are:
0069 006e 0064 0069 0067 006f
*/

注解

参数指定的 culture 区域性的大小写规则决定了字符串大小写的更改方式。

备注

此方法不会修改当前实例的值。 相反,它返回一个新字符串,其中当前实例中的所有字符都转换为小写。

如果向 ToLower(CultureInfo) 方法 CultureInfo 传递对象而不是 CultureInfo.InvariantCulture,则大小写操作将考虑区域性特定的规则。 如果需要操作系统标识符的小写或大写版本(如文件名、命名管道或注册表项),请使用 ToLowerInvariantToUpperInvariant 方法。 这在每个区域性中产生相同的结果,并且执行效率更高。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1