to_upper
2026/4/14大约 3 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — to_upper
to_upper
定义
to_upper 用来把字符串中的所有小写字母转换成大写字母。你可以把它想象成"让所有字母都站直"——小写的 a 变成大写的 A,z 变成 Z。非字母字符(数字、中文、标点)不受影响。
这个方法在显示标题、标准化标识符、做大小写不敏感比较时非常常用。
函数签名
C#
// Godot.StringExtensions 方法
public static string ToUpper(this string instance)GDScript
func to_upper() -> String参数说明
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| 无 | — | — | 这是一个无参数方法,直接对字符串本身操作 |
返回值
类型: string / String
返回所有字母都转为大写的新字符串。原字符串不会被修改。
代码示例
基础用法
C#
using Godot;
public partial class ToUpperExample : Node
{
public override void _Ready()
{
string text = "Hello, godot 4.6!";
GD.Print(text.ToUpper());
// 运行结果: HELLO, GODOT 4.6!
// 已经是大写的字符串不受影响
GD.Print("ALREADY UPPER".ToUpper());
// 运行结果: ALREADY UPPER
// 数字和标点不受影响
GD.Print("abc123!@#".ToUpper());
// 运行结果: ABC123!@#
}
}GDScript
extends Node
func _ready():
var text := "Hello, godot 4.6!"
print(text.to_upper())
# 运行结果: HELLO, GODOT 4.6!
# 已经是大写的字符串不受影响
print("ALREADY UPPER".to_upper())
# 运行结果: ALREADY UPPER
# 数字和标点不受影响
print("abc123!@#".to_upper())
# 运行结果: ABC123!@#实际场景:显示游戏标题和等级标签
C#
using Godot;
public partial class UIDisplay : Node
{
public override void _Ready()
{
// 将等级标签显示为大写
string rarity = "legendary";
GD.Print($"稀有度: [{rarity.ToUpper()}]");
// 运行结果: 稀有度: [LEGENDARY]
// 游戏标题标准化显示
string gameTitle = "evocosmos";
GD.Print($"=== {gameTitle.ToUpper()} ===");
// 运行结果: === EVOCOSMOS ===
// 状态提示信息
string[] statuses = { "pause", "resume", "game over" };
foreach (string status in statuses)
{
GD.Print($">> {status.ToUpper()} <<");
}
// 运行结果:
// >> PAUSE <<
// >> RESUME <<
// >> GAME OVER <<
}
}GDScript
extends Node
func _ready():
# 将等级标签显示为大写
var rarity := "legendary"
print("稀有度: [" + rarity.to_upper() + "]")
# 运行结果: 稀有度: [LEGENDARY]
# 游戏标题标准化显示
var game_title := "evocosmos"
print("=== " + game_title.to_upper() + " ===")
# 运行结果: === EVOCOSMOS ===
# 状态提示信息
var statuses := ["pause", "resume", "game over"]
for status in statuses:
print(">> " + status.to_upper() + " <<")
# 运行结果:
# >> PAUSE <<
# >> RESUME <<
# >> GAME OVER <<进阶用法:枚举名称生成与常量命名
C#
using Godot;
public partial class ToUpperAdvanced : Node
{
public override void _Ready()
{
// 生成常量名称(全大写 + 下划线)
string[] configKeys = { "maxHealth", "moveSpeed", "jumpForce" };
foreach (string key in configKeys)
{
string constName = key.ToSnakeCase().ToUpper();
GD.Print($"const {constName} = ...;");
}
// 运行结果:
// const MAX_HEALTH = ...;
// const MOVE_SPEED = ...;
// const JUMP_FORCE = ...;
// 大小写不敏感的枚举匹配
string input = "fire";
string[] elements = { "Fire", "Water", "Earth", "Wind" };
foreach (string element in elements)
{
if (element.ToUpper() == input.ToUpper())
{
GD.Print($"匹配到元素: {element}");
}
}
// 运行结果: 匹配到元素: Fire
}
}GDScript
extends Node
func _ready():
# 生成常量名称(全大写 + 下划线)
var config_keys := ["maxHealth", "moveSpeed", "jumpForce"]
for key in config_keys:
var const_name := key.to_snake_case().to_upper()
print("const " + const_name + " = ...")
# 运行结果:
# const MAX_HEALTH = ...
# const MOVE_SPEED = ...
# const JUMP_FORCE = ...
# 大小写不敏感的枚举匹配
var input := "fire"
var elements := ["Fire", "Water", "Earth", "Wind"]
for element in elements:
if element.to_upper() == input.to_upper():
print("匹配到元素: " + element)
# 运行结果: 匹配到元素: Fire注意事项
- 只影响字母:数字、标点符号、中文字符等不受影响,保持原样。
- 不修改原字符串:返回一个新字符串,原字符串保持不变。
- C# 内置方法同名:C# 自带的
string.ToUpper()和 Godot 的ToUpper()扩展方法功能一致。在 C# 中调用时,两者等效。 - 与
to_lower互补:to_upper转大写,to_lower转小写。两者配合可以实现大小写标准化。 - 不适用
StringName:Godot 的StringName类型没有ToUpper方法。如果需要转大写,先转为string再调用。
