C#如何使用字符串中的语句来解析文本
C# How to use Statements in strings to parse text
我从事基于文本的游戏已有一段时间了,我 运行 遇到的一个问题是,当我必须根据变量编写不同的段落时,这些变量可能会在整个过程中发生变化游戏。
我四处寻找解析器等,但主要是找到正则表达式,我认为它在这里没有用。我正在寻找的是某种方式来查看这样的字符串。
String x = "'It's nice to meet you [if (Female){ "Miss, what's a pretty young thing like you doing out in the dessert."} else { "Sir, what can I do for ya?"}]' the man asks in a drawl.";
我目前必须写出用 if 语句打断的段落,但这样写会花费更长的时间,并且让谈话在我脑海中保持流畅变得更加困难。我的目标是 class 我可以根据在整个游戏中变化的变量在执行时发送字符串并取回格式化版本。
有多种方法可以完成您想要的,但这里是一种方法。有一个 class 可以根据性别为每个对话生成回复:
class CharacterResponses
{
private bool _isFemale;
new CharacterResponses(bool isFemale)
{
_isFemale = isFemale;
}
public string GetResponse1()
{
return _isFemale
? "Miss, what's a pretty young thing like you doing out in the dessert."
: "Sir, what can I do for ya?";
}
public string GetResponse2() // For a different conversation.
// etc...
}
现在在你的程序中,你可以有这样的流程:
var character = new CharacterResponses(true); // Female.
var x = string.Format("It's nice to meet you {0} the man asks in a drawl.", character.GetResponse1());
这会将所有 if
- then
特定于角色性别的逻辑保留在其自己的代码块中,并允许您轻松地将占位符放入主要对话中。
如果这是您的目标,那么 c# 确实不适合您。它根本不提供此功能,虽然它 will offer string interpolation in version 6.0,但我怀疑它是否会像您想要的那样支持完整语句。这种功能根本不是语言的目标,实际上会使主要目标之一(一流的工具支持)变得更加困难。
一个有趣的替代方法可能是编写在动态的 powershell 中执行插值的代码,supports full string interpolation,自 Windows XP 3 以来安装在每台 Windows 机器上,并且与任何 .Net 语言本机互操作。
您甚至可以通过 calling powershell for just that one component.
在 c# 中编写其余代码来做到这一点
由于 ifs 的 powershell 语法看起来与 c# 非常相似,因此您的代码可能类似于
interpolate(@"'It's nice to meet you $(if (Female){ ""Miss, what's a pretty
young thing like you doing out in the dessert.""} else { ""Sir, what can I
do for ya?""})' the man asks in a drawl.");
话虽这么说,但这样做并不是超级简单,但如果必须从 c# 代码文件进行字符串插值,这可能是您的最佳选择。
其他选择:
- 同样的事情,但使用任何其他支持插值的 .Net 语言而不是 Powershell
- 将您的字符串编写为 Razor 模板并使用 RazorEngine 编译它们
- 使用即将推出的 Roslyn 编译器的功能将您的字符串编译为 c#。
None这东西比较简单,但是都可以。
如果您有很多文本和很多可能的变体,我建议您编写一些代码来为您生成字符串。无论如何,将它们混在源代码中似乎不太好(恕我直言)。
一些 xml 怎么样?
<Message Key="message123">
<Text>It's nice to meet you </Text>
<Switch Field="Gender">
<Female>
Miss, what's a pretty young thing like you
doing out in the dessert?
</Female>
<Male>Sir, what can I do for ya?</Male>
</Switch>
<Text> the man asks in a drawl</Text>
</Message>
为 <If>
语句和其他内容添加一些代码,您将拥有很大的灵活性。 IE。基于其他变量的不同文本,例如健康或库存项目。
不是你问题的真正答案,但我想无论如何我都会加两分钱。
写 class StringInterpolated
(名称并不重要),它插入字符串(以您的格式)。唯一的缺点是您必须在末尾附加参数 - 在您的示例中 Female
.
根据您的需要,您可以抛出任何语法来插入您想要的字符串,您还可以添加检查是否传递了所有带值的参数,这样这一面就会被覆盖。
我从事基于文本的游戏已有一段时间了,我 运行 遇到的一个问题是,当我必须根据变量编写不同的段落时,这些变量可能会在整个过程中发生变化游戏。
我四处寻找解析器等,但主要是找到正则表达式,我认为它在这里没有用。我正在寻找的是某种方式来查看这样的字符串。
String x = "'It's nice to meet you [if (Female){ "Miss, what's a pretty young thing like you doing out in the dessert."} else { "Sir, what can I do for ya?"}]' the man asks in a drawl.";
我目前必须写出用 if 语句打断的段落,但这样写会花费更长的时间,并且让谈话在我脑海中保持流畅变得更加困难。我的目标是 class 我可以根据在整个游戏中变化的变量在执行时发送字符串并取回格式化版本。
有多种方法可以完成您想要的,但这里是一种方法。有一个 class 可以根据性别为每个对话生成回复:
class CharacterResponses
{
private bool _isFemale;
new CharacterResponses(bool isFemale)
{
_isFemale = isFemale;
}
public string GetResponse1()
{
return _isFemale
? "Miss, what's a pretty young thing like you doing out in the dessert."
: "Sir, what can I do for ya?";
}
public string GetResponse2() // For a different conversation.
// etc...
}
现在在你的程序中,你可以有这样的流程:
var character = new CharacterResponses(true); // Female.
var x = string.Format("It's nice to meet you {0} the man asks in a drawl.", character.GetResponse1());
这会将所有 if
- then
特定于角色性别的逻辑保留在其自己的代码块中,并允许您轻松地将占位符放入主要对话中。
如果这是您的目标,那么 c# 确实不适合您。它根本不提供此功能,虽然它 will offer string interpolation in version 6.0,但我怀疑它是否会像您想要的那样支持完整语句。这种功能根本不是语言的目标,实际上会使主要目标之一(一流的工具支持)变得更加困难。
一个有趣的替代方法可能是编写在动态的 powershell 中执行插值的代码,supports full string interpolation,自 Windows XP 3 以来安装在每台 Windows 机器上,并且与任何 .Net 语言本机互操作。
您甚至可以通过 calling powershell for just that one component.
在 c# 中编写其余代码来做到这一点由于 ifs 的 powershell 语法看起来与 c# 非常相似,因此您的代码可能类似于
interpolate(@"'It's nice to meet you $(if (Female){ ""Miss, what's a pretty
young thing like you doing out in the dessert.""} else { ""Sir, what can I
do for ya?""})' the man asks in a drawl.");
话虽这么说,但这样做并不是超级简单,但如果必须从 c# 代码文件进行字符串插值,这可能是您的最佳选择。
其他选择:
- 同样的事情,但使用任何其他支持插值的 .Net 语言而不是 Powershell
- 将您的字符串编写为 Razor 模板并使用 RazorEngine 编译它们
- 使用即将推出的 Roslyn 编译器的功能将您的字符串编译为 c#。
None这东西比较简单,但是都可以。
如果您有很多文本和很多可能的变体,我建议您编写一些代码来为您生成字符串。无论如何,将它们混在源代码中似乎不太好(恕我直言)。
一些 xml 怎么样?
<Message Key="message123">
<Text>It's nice to meet you </Text>
<Switch Field="Gender">
<Female>
Miss, what's a pretty young thing like you
doing out in the dessert?
</Female>
<Male>Sir, what can I do for ya?</Male>
</Switch>
<Text> the man asks in a drawl</Text>
</Message>
为 <If>
语句和其他内容添加一些代码,您将拥有很大的灵活性。 IE。基于其他变量的不同文本,例如健康或库存项目。
不是你问题的真正答案,但我想无论如何我都会加两分钱。
写 class StringInterpolated
(名称并不重要),它插入字符串(以您的格式)。唯一的缺点是您必须在末尾附加参数 - 在您的示例中 Female
.
根据您的需要,您可以抛出任何语法来插入您想要的字符串,您还可以添加检查是否传递了所有带值的参数,这样这一面就会被覆盖。