TextInfo.ToUpper 方法

定义

将指定的字符或字符串转换为大写。Converts the specified character or string to uppercase.

重载

ToUpper(Char)

将指定的字符转换为大写。Converts the specified character to uppercase.

ToUpper(String)

将指定的字符串转换为大写。Converts the specified string to uppercase.

ToUpper(Char)

将指定的字符转换为大写。Converts the specified character to uppercase.

public:
 virtual char ToUpper(char c);
public:
 char ToUpper(char c);
public virtual char ToUpper (char c);
public char ToUpper (char c);
abstract member ToUpper : char -> char
override this.ToUpper : char -> char
member this.ToUpper : char -> char
Public Overridable Function ToUpper (c As Char) As Char
Public Function ToUpper (c As Char) As Char

参数

c
Char

要转换为大写的字符。The character to convert to uppercase.

返回

Char

转换为大写的指定字符。The specified character converted to uppercase.

示例

下面的代码示例根据英语 (美国) 区域性,使用区域性名称 en-us 更改字符串的大小写。The following code example changes the casing of a string based on the English (United States) culture, with the culture name en-US.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the String* with mixed casing.
   String^ myString = "wAr aNd pEaCe";
   
   // Creates a TextInfo based on the S"en-US" culture.
   CultureInfo^ MyCI = gcnew CultureInfo( "en-US",false );
   TextInfo^ myTI = MyCI->TextInfo;
   
   // Changes a String* to lowercase.
   Console::WriteLine( "\"{0}\" to lowercase: {1}", myString, myTI->ToLower( myString ) );
   
   // Changes a String* to uppercase.
   Console::WriteLine( "\"{0}\" to uppercase: {1}", myString, myTI->ToUpper( myString ) );
   
   // Changes a String* to titlecase.
   Console::WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI->ToTitleCase( myString ) );
}

/*
This code produces the following output.

S"wAr aNd pEaCe" to lowercase: war and peace
S"wAr aNd pEaCe" to uppercase: WAR AND PEACE
S"wAr aNd pEaCe" to titlecase: War And Peace

*/
using System;
using System.Globalization;

public class SamplesTextInfo  {

   public static void Main()  {

      // Defines the string with mixed casing.
      string myString = "wAr aNd pEaCe";

      // Creates a TextInfo based on the "en-US" culture.
      TextInfo myTI = new CultureInfo("en-US",false).TextInfo;

      // Changes a string to lowercase.
      Console.WriteLine( "\"{0}\" to lowercase: {1}", myString, myTI.ToLower( myString ) );

      // Changes a string to uppercase.
      Console.WriteLine( "\"{0}\" to uppercase: {1}", myString, myTI.ToUpper( myString ) );

      // Changes a string to titlecase.
      Console.WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase( myString ) );
   }
}

/*
This code produces the following output.

"wAr aNd pEaCe" to lowercase: war and peace
"wAr aNd pEaCe" to uppercase: WAR AND PEACE
"wAr aNd pEaCe" to titlecase: War And Peace

*/
Imports System.Globalization

Public Class SamplesTextInfo

   Public Shared Sub Main()

      ' Defines the string with mixed casing.
      Dim myString As String = "wAr aNd pEaCe"

      ' Creates a TextInfo based on the "en-US" culture.
      Dim myTI As TextInfo = New CultureInfo("en-US", False).TextInfo

      ' Changes a string to lowercase.
      Console.WriteLine("""{0}"" to lowercase: {1}", myString, myTI.ToLower(myString))

      ' Changes a string to uppercase.
      Console.WriteLine("""{0}"" to uppercase: {1}", myString, myTI.ToUpper(myString))

      ' Changes a string to titlecase.
      Console.WriteLine("""{0}"" to titlecase: {1}", myString, myTI.ToTitleCase(myString))

   End Sub

End Class


'This code produces the following output.
'
'"wAr aNd pEaCe" to lowercase: war and peace
'"wAr aNd pEaCe" to uppercase: WAR AND PEACE
'"wAr aNd pEaCe" to titlecase: War And Peace


注解

大小写语义取决于所使用的区域性。Casing semantics depend on the culture in use. 对于固定区域性,大小写语义不区分区域性。For the invariant culture, the casing semantics are not culture-sensitive. 对于特定区域性,大小写语义对该区域性是敏感的。For a specific culture, the casing semantics are sensitive to that culture.

如果安全决策取决于字符串比较或大小写更改操作,则应用程序应使用 InvariantCulture 来确保行为一致,而不管系统的区域性设置如何。If a security decision depends on a string comparison or a case-change operation, the application should use the InvariantCulture to ensure that the behavior is consistent regardless of the culture settings of the system. 但是,固定区域性只能由需要与区域性无关的结果的进程(如系统服务)使用。However, the invariant culture must be used only by processes that require culture-independent results, such as system services. 否则,它将生成可能是语言不正确或区域性不适当的结果。Otherwise, it produces results that might be linguistically incorrect or culturally inappropriate.

有关区域性的详细信息,请参阅 CultureInfoFor more information on cultures, see CultureInfo.

另请参阅

适用于

ToUpper(String)

将指定的字符串转换为大写。Converts the specified string to uppercase.

public:
 virtual System::String ^ ToUpper(System::String ^ str);
public:
 System::String ^ ToUpper(System::String ^ str);
public virtual string ToUpper (string str);
public string ToUpper (string str);
abstract member ToUpper : string -> string
override this.ToUpper : string -> string
member this.ToUpper : string -> string
Public Overridable Function ToUpper (str As String) As String
Public Function ToUpper (str As String) As String

参数

str
String

要转换为大写的字符串。The string to convert to uppercase.

返回

String

转换为大写的指定字符串。The specified string converted to uppercase.

例外

str 为 null。str is null.

示例

下面的代码示例根据英语 (美国) 区域性,使用区域性名称 en-us 更改字符串的大小写。The following code example changes the casing of a string based on the English (United States) culture, with the culture name en-US.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the String* with mixed casing.
   String^ myString = "wAr aNd pEaCe";
   
   // Creates a TextInfo based on the S"en-US" culture.
   CultureInfo^ MyCI = gcnew CultureInfo( "en-US",false );
   TextInfo^ myTI = MyCI->TextInfo;
   
   // Changes a String* to lowercase.
   Console::WriteLine( "\"{0}\" to lowercase: {1}", myString, myTI->ToLower( myString ) );
   
   // Changes a String* to uppercase.
   Console::WriteLine( "\"{0}\" to uppercase: {1}", myString, myTI->ToUpper( myString ) );
   
   // Changes a String* to titlecase.
   Console::WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI->ToTitleCase( myString ) );
}

/*
This code produces the following output.

S"wAr aNd pEaCe" to lowercase: war and peace
S"wAr aNd pEaCe" to uppercase: WAR AND PEACE
S"wAr aNd pEaCe" to titlecase: War And Peace

*/
using System;
using System.Globalization;

public class SamplesTextInfo  {

   public static void Main()  {

      // Defines the string with mixed casing.
      string myString = "wAr aNd pEaCe";

      // Creates a TextInfo based on the "en-US" culture.
      TextInfo myTI = new CultureInfo("en-US",false).TextInfo;

      // Changes a string to lowercase.
      Console.WriteLine( "\"{0}\" to lowercase: {1}", myString, myTI.ToLower( myString ) );

      // Changes a string to uppercase.
      Console.WriteLine( "\"{0}\" to uppercase: {1}", myString, myTI.ToUpper( myString ) );

      // Changes a string to titlecase.
      Console.WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase( myString ) );
   }
}

/*
This code produces the following output.

"wAr aNd pEaCe" to lowercase: war and peace
"wAr aNd pEaCe" to uppercase: WAR AND PEACE
"wAr aNd pEaCe" to titlecase: War And Peace

*/
Imports System.Globalization

Public Class SamplesTextInfo

   Public Shared Sub Main()

      ' Defines the string with mixed casing.
      Dim myString As String = "wAr aNd pEaCe"

      ' Creates a TextInfo based on the "en-US" culture.
      Dim myTI As TextInfo = New CultureInfo("en-US", False).TextInfo

      ' Changes a string to lowercase.
      Console.WriteLine("""{0}"" to lowercase: {1}", myString, myTI.ToLower(myString))

      ' Changes a string to uppercase.
      Console.WriteLine("""{0}"" to uppercase: {1}", myString, myTI.ToUpper(myString))

      ' Changes a string to titlecase.
      Console.WriteLine("""{0}"" to titlecase: {1}", myString, myTI.ToTitleCase(myString))

   End Sub

End Class


'This code produces the following output.
'
'"wAr aNd pEaCe" to lowercase: war and peace
'"wAr aNd pEaCe" to uppercase: WAR AND PEACE
'"wAr aNd pEaCe" to titlecase: War And Peace


注解

返回的字符串的长度可能不同于输入字符串。The returned string might differ in length from the input string. 有关大小写的详细信息,请参阅 unicode 技术报告 #21 "案例映射", (Unicode 协会发布 https://www.unicode.org) 。For more information on casing, refer to the Unicode Technical Report #21 "Case Mappings," published by the Unicode Consortium (https://www.unicode.org). 当前实现保留字符串的长度。The current implementation preserves the length of the string. 但是,这种行为并不保证,在将来的实现中可能会发生变化。However, this behavior is not guaranteed and could change in future implementations.

大小写语义取决于所使用的区域性。Casing semantics depend on the culture in use. 对于固定区域性,大小写语义不区分区域性。For the invariant culture, the casing semantics are not culture-sensitive. 对于特定区域性,大小写语义对该区域性是敏感的。For a specific culture, the casing semantics are sensitive to that culture.

如果安全决策取决于字符串比较或大小写更改操作,则应用程序应使用 InvariantCulture 来确保行为一致,而不管系统的区域性设置如何。If a security decision depends on a string comparison or a case-change operation, the application should use the InvariantCulture to ensure that the behavior is consistent regardless of the culture settings of the system. 但是,固定区域性只能由需要与区域性无关的结果的进程(如系统服务)使用。However, the invariant culture must be used only by processes that require culture-independent results, such as system services. 否则,它将生成可能是语言不正确或区域性不适当的结果。Otherwise, it produces results that might be linguistically incorrect or culturally inappropriate.

有关区域性的详细信息,请参阅 CultureInfoFor more information on cultures, see CultureInfo.

另请参阅

适用于