C# String ToUpper()

String 的 ToUpper() 方法将字符串中的所有字符转换为大写。

示例

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "chocolate";
     
// converts str to upper case string result = str.ToUpper();
Console.WriteLine(result); Console.ReadLine(); } } } // Output: CHOCOLATE

ToUpper() 语法

字符串 ToUpper() 方法的语法是

ToUpper()

这里,ToUpper()String 类的一个方法。


ToUpper() 返回值

ToUpper() 方法返回

  • 转换成大写后字符串的副本

示例 1:C# String ToUpper()

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Ice Cream";
     
// converts str to upper case string result = str.ToUpper();
Console.WriteLine(result); Console.ReadLine(); } } }

输出

ICE CREAM

这里,str.ToUpper()str 中的字母转换为大写。


带 CultureInfo 参数的 ToUpper()

我们也可以将 CultureInfo 作为参数传递给 ToUpper() 方法。CultureInfo 允许我们使用指定区域设置的大小写规则。

其语法为

ToUpper(System.Globalization.CultureInfo culture)

这里,

culture - 提供特定于区域设置的大小写规则


示例 2:带 CultureInfo 的 C# String ToUpper()

using System;  
using System.Globalization;
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "ice cream";
     
// converts str to uppercase in Turkish-Turkey culture string result = str.ToUpper(new CultureInfo("tr-TR", false));
Console.WriteLine(result); Console.ReadLine(); } } }

输出

İCE CREAM

在上面的程序中,请注意以下代码:

str.ToUpper(new CultureInfo("tr-TR", false))

在这里,我们对 str 使用了 Turkish-Turkey 区域设置的大小写规则。这由以下 CultureInfo() 参数给出

  • tr-TR - 使用 Turkish-Turkey 区域设置
  • false - 表示默认区域设置

结果是,小写字母 "i" 被转换为土耳其语的 "İ" 而不是美式英语的 "I"

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战