find
2026/4/14大约 4 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — find
find
定义
find 用来在一个字符串里查找另一个子字符串首次出现的位置。你可以把它想象成在一段文字里"搜索关键词"——它会告诉你关键词从第几个字符开始出现。如果找不到,就返回 -1。
所谓"位置",就是字符在字符串中的编号,从 0 开始数。比如字符串 "Hello" 中,'H' 的位置是 0,'e' 的位置是 1,以此类推。
函数签名
C#
// Godot.StringExtensions 方法
public static int Find(this string instance, string what, int from = 0)GDScript
func find(what: String, from: int = 0) -> int参数说明
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
what | string / String | 是 | — | 要查找的子字符串,即你想搜的那个"关键词" |
from | int | 否 | 0 | 从第几个字符开始找。默认从开头(位置 0)开始 |
返回值
类型: int
- 如果找到了子字符串,返回它在原字符串中首次出现的位置(从 0 开始计数)
- 如果没找到,返回
-1
代码示例
基础用法
C#
using Godot;
public partial class FindExample : Node
{
public override void _Ready()
{
string text = "Hello, Godot!";
// 查找 "Godot" 出现的位置
int pos = text.Find("Godot");
GD.Print(pos);
// 运行结果: 7
// 查找一个不存在的子字符串
int notFound = text.Find("Unity");
GD.Print(notFound);
// 运行结果: -1
}
}GDScript
extends Node
func _ready():
var text := "Hello, Godot!"
# 查找 "Godot" 出现的位置
var pos := text.find("Godot")
print(pos)
# 运行结果: 7
# 查找一个不存在的子字符串
var not_found := text.find("Unity")
print(not_found)
# 运行结果: -1实际场景:解析对话文本中的角色名
C#
using Godot;
public partial class DialogueParser : Node
{
public override void _Ready()
{
string dialogue = "[Knight]: We must defend the castle!";
// 找到角色名标记的起止位置
int start = dialogue.Find("[") + 1; // 跳过 '['
int end = dialogue.Find("]");
if (start > 0 && end > start)
{
string speaker = dialogue.Substring(start, end - start);
string message = dialogue.Substring(end + 2); // 跳过 "]: "
GD.Print($"说话者: {speaker}");
GD.Print($"内容: {message}");
}
// 运行结果:
// 说话者: Knight
// 内容: We must defend the castle!
}
}GDScript
extends Node
func _ready():
var dialogue := "[Knight]: We must defend the castle!"
# 找到角色名标记的起止位置
var start := dialogue.find("[") + 1 # 跳过 '['
var end := dialogue.find("]")
if start > 0 and end > start:
var speaker := dialogue.substr(start, end - start)
var message := dialogue.substr(end + 2) # 跳过 "]: "
print("说话者: " + speaker)
print("内容: " + message)
# 运行结果:
# 说话者: Knight
# 内容: We must defend the castle!进阶用法:使用 from 参数跳过已匹配项
C#
using Godot;
public partial class FindAdvanced : Node
{
public override void _Ready()
{
string csv = "apple,banana,apple,cherry";
// 第一次查找 "apple"
int first = csv.Find("apple");
GD.Print($"第一个 apple 位置: {first}");
// 从 first + "apple".Length 开始,跳过第一个,查找第二个
int second = csv.Find("apple", first + "apple".Length);
GD.Print($"第二个 apple 位置: {second}");
// 也可以用 rfind 从右往左找(返回最后出现的位置)
int last = csv.RFind("apple");
GD.Print($"最后一个 apple 位置: {last}");
// 运行结果:
// 第一个 apple 位置: 0
// 第二个 apple 位置: 13
// 最后一个 apple 位置: 13
}
}GDScript
extends Node
func _ready():
var csv := "apple,banana,apple,cherry"
# 第一次查找 "apple"
var first := csv.find("apple")
print("第一个 apple 位置: " + str(first))
# 从 first + "apple".length() 开始,跳过第一个,查找第二个
var second := csv.find("apple", first + "apple".length())
print("第二个 apple 位置: " + str(second))
# 也可以用 rfind 从右往左找(返回最后出现的位置)
var last := csv.rfind("apple")
print("最后一个 apple 位置: " + str(last))
# 运行结果:
# 第一个 apple 位置: 0
# 第二个 apple 位置: 13
# 最后一个 apple 位置: 13注意事项
- 区分大小写:
find是区分大小写的。"Hello".Find("hello")会返回-1。如果需要不区分大小写,可以先将两个字符串都转为小写再查找,或者使用FindN(GDScript 中为findn)方法。 - 返回
-1表示未找到:在调用find后,务必检查返回值是否为-1,再根据结果进行后续操作,避免使用-1作为索引导致逻辑错误。 from参数不能为负数:如果传入负数的from,搜索行为未定义,请始终确保from >= 0。- C# 中的替代方案:在 C# 中,你也可以使用 .NET 自带的
string.IndexOf()方法,功能类似。但Find是 Godot 提供的扩展方法,在处理 Godot 的StringName等类型时更方便。 - 空字符串查找:查找空字符串
""时,返回0(即开头位置),这是符合直觉的行为。
