strip_edges
2026/4/14大约 3 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — strip_edges
strip_edges
定义
strip_edges 用来去除字符串开头和末尾的空白字符(如空格、制表符、换行符)。你可以把它想象成"给字符串理发"——把前后多余的"头发"(空白)剪掉,中间的内容保持不变。
这在处理用户输入、读取文件内容时非常常用,因为用户或文件经常会在前后多出一些看不见的空白。
函数签名
C#
// Godot.StringExtensions 方法
public static string StripEdges(this string instance, bool left = true, bool right = true)GDScript
func strip_edges(left: bool = true, right: bool = true) -> String参数说明
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
left | bool | 否 | true | 是否去除左边的空白。true 去除,false 保留 |
right | bool | 否 | true | 是否去除右边的空白。true 去除,false 保留 |
返回值
类型: string / String
返回去除两端空白后的新字符串。原字符串不会被修改。
代码示例
基础用法
C#
using Godot;
public partial class StripEdgesExample : Node
{
public override void _Ready()
{
string text = " Hello, Godot! \n";
// 默认:去除两端空白
GD.Print($"[{text.StripEdges()}]");
// 运行结果: [Hello, Godot!]
// 只去除左边
GD.Print($"[{text.StripEdges(true, false)}]");
// 运行结果: [Hello, Godot! \n]
// 只去除右边
GD.Print($"[{text.StripEdges(false, true)}]");
// 运行结果: [ Hello, Godot!]
}
}GDScript
extends Node
func _ready():
var text := " Hello, Godot! \n"
# 默认:去除两端空白
print("[" + text.strip_edges() + "]")
# 运行结果: [Hello, Godot!]
# 只去除左边
print("[" + text.strip_edges(true, false) + "]")
# 运行结果: [Hello, Godot!
# ]
# 只去除右边
print("[" + text.strip_edges(false, true) + "]")
# 运行结果: [ Hello, Godot!]实际场景:清理用户输入
C#
using Godot;
public partial class InputCleaner : Node
{
public override void _Ready()
{
// 模拟用户输入时不小心多打了空格
string username = " 勇者小明 ";
string password = " mypassword\n";
// 清理后再判断
username = username.StripEdges();
password = password.StripEdges();
if (username.IsEmpty())
{
GD.Print("用户名不能为空!");
}
else
{
GD.Print($"欢迎,{username}!");
}
// 运行结果: 欢迎,勇者小明!
}
}GDScript
extends Node
func _ready():
# 模拟用户输入时不小心多打了空格
var username := " 勇者小明 "
var password := " mypassword\n"
# 清理后再判断
username = username.strip_edges()
password = password.strip_edges()
if username.is_empty():
print("用户名不能为空!")
else:
print("欢迎," + username + "!")
# 运行结果: 欢迎,勇者小明!进阶用法:处理多行文本
C#
using Godot;
public partial class StripEdgesAdvanced : Node
{
public override void _Ready()
{
// 逐行清理多行文本
string text = " line one \n line two \n line three ";
string[] lines = text.Split("\n");
GD.Print("=== 清理后的文本 ===");
foreach (string line in lines)
{
string cleaned = line.StripEdges();
GD.Print(cleaned);
}
// 运行结果:
// === 清理后的文本 ===
// line one
// line two
// line three
// strip_edges 只去除两端,不影响中间的空白
string middle = "Hello World";
GD.Print(middle.StripEdges());
// 运行结果: Hello World(中间空格保留不变)
// 区别于 strip_escapes:strip_edges 不处理转义字符
string escaped = " \\t\\n ";
GD.Print($"[{escaped.StripEdges()}]");
// 运行结果: [\t\n](去掉了空格,转义字面量保留)
}
}GDScript
extends Node
func _ready():
# 逐行清理多行文本
var text := " line one \n line two \n line three "
var lines := text.split("\n")
print("=== 清理后的文本 ===")
for line in lines:
var cleaned := line.strip_edges()
print(cleaned)
# 运行结果:
# === 清理后的文本 ===
# line one
# line two
# line three
# strip_edges 只去除两端,不影响中间的空白
var middle := "Hello World"
print(middle.strip_edges())
# 运行结果: Hello World(中间空格保留不变)注意事项
- 只去除两端:
strip_edges只去除开头和末尾的空白字符,字符串中间的空白不受影响。 - 去除的空白类型:包括空格 (
)、制表符 (\t)、换行符 (\n)、回车符 (\r) 等所有 Unicode 空白字符。 - 左右可独立控制:通过
left和right参数可以只去除一边的空白,非常灵活。 - 不修改原字符串:返回新字符串,原字符串保持不变。
- C# 替代方案:C# 的
string.Trim()功能等价于StripEdges(),TrimStart()等价于StripEdges(true, false),TrimEnd()等价于StripEdges(false, true)。 - 常与
is_empty配合:先用strip_edges清理,再用is_empty判断是否为空,这是处理用户输入的黄金组合。
