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"
。