C# String ToUpper() 字符串转大写方法
字符串的 ToUpper() 方法将字符串中的所有字符转换为大写字母。
示例
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str = "巧克力";
// 将str转换为大写
string result = str.ToUpper();
Console.WriteLine(result);
Console.ReadLine();
}
}
}
// 输出: 巧克力
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 = "冰淇淋";
// 将str转换为大写
string result = str.ToUpper();
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
冰淇淋
在这里,str.ToUpper() 将 str 中的字母转换为大写。
使用 CultureInfo 参数的 ToUpper()
我们还可以将 CultureInfo 作为参数传递给 ToUpper() 方法。CultureInfo 允许我们使用指定文化的大小写规则。
其语法如下:
ToUpper(System.Globalization.CultureInfo culture)
在这里,
culture - 提供特定文化的大小写规则
示例 2: C# String ToUpper() 使用 CultureInfo
using System;
using System.Globalization;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str = "ice cream";
// 在土耳其-土耳其文化中将str转换为大写
string result = str.ToUpper(new CultureInfo("tr-TR", false));
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
İCE CREAM
在上面的程序中,请注意以下代码:
str.ToUpper(new CultureInfo("tr-TR", false))
在这里,我们使用土耳其-土耳其文化的大小写规则处理 str。这由以下 CultureInfo() 参数给出:
tr-TR- 使用土耳其-土耳其文化false- 表示默认文化设置
因此,小写的 "i" 被转换为土耳其文化的 "İ",而不是美式英文的 "I"。