一般使用 indexOf
有很多方法可以做到这一点,但是您确实需要使用 indexOf。
int openBrace = originalString.indexOf("{");
如果 openBrace 为 -1,则未找到。如果您不想处理所有大括号,那么您将需要使用额外的参数选项。
int closingBrace = 0;
int openBrace = 0;
//Loop forever, break logic is inside
while (true) {
openBrace = originalString.indexOf("{", closingBrace);
if (openBrace == -1) break;
closingBrace = originalString.indexOf("}", openBrace);
if (closingBrace == -1) closingBrace = originalString.Length;
openBrace++; //Doing this, makes the next statement look nice
string BraceContents = originalString.SubString(openBrace, closingBrace - openBrace);
}
(可能存在错误,所以如果您需要帮助改进它,请告诉我)
我希望你能明白。您当前使用 split 和 contains 的过程非常有限。
建议的脚本解析架构
但是,为了提高整个脚本处理系统的性能和简单性,您可能希望一次处理一个字符,然后输出到 StringBuilder。
一般使用 indexOf
有很多方法可以做到这一点,但是您确实需要使用 indexOf。
int openBrace = originalString.indexOf("{");
如果 openBrace 为 -1,则未找到。如果您不想处理所有大括号,那么您将需要使用额外的参数选项。
int closingBrace = 0;
int openBrace = 0;
//Loop forever, break logic is inside
while (true) {
openBrace = originalString.indexOf("{", closingBrace);
if (openBrace == -1) break;
closingBrace = originalString.indexOf("}", openBrace);
if (closingBrace == -1) closingBrace = originalString.Length;
openBrace++; //Doing this, makes the next statement look nice
string BraceContents = originalString.SubString(openBrace, closingBrace - openBrace);
}
(可能存在错误,所以如果您需要帮助改进它,请告诉我)
我希望你能明白。您当前使用 split 和 contains 的过程非常有限。
建议的脚本解析架构
但是,为了提高整个脚本处理系统的性能和简单性,您可能希望一次处理一个字符,然后输出到 StringBuilder。
class ScriptParser
{
//Declare as member variables, to make sub-function calls simpler (not need to have "ref i", for example)
StringBuilder sb = new StringBuilder();
int i;
string scriptString;
public string Parse(string input)
{
scriptString = input; //Share across object
//Loop through each character one at a time once
for (i = 0; i < scriptString.Length; i++)
{
//I suggest naming your conditions like this, especially if you're going to have more types of commands, and escape sequences in the future.
bool isRandomCommand = (scriptString[i] == '{'); //What you have described
bool isSomeOtherCommand = (scriptString[i] == '['); //Dummy
bool isCommand = isRandomCommand || isSomeOtherCommand; //For later, determines whether we continue, bypassing the direct copy-through default
//Command processing
if (isRandomCommand) //TODO: perhaps detect if the next character is double brace, in which case it's an escape sequence and we should output '{'
ProcessRandomCommand(); //This function will automatically update i to the end of the brace
else if (isSomeOtherCommand) //Dummy
ProcessSomeOtherCommand(); //Dummy
if (isCommand)
continue; //The next character could be another {} section, so re-evaluate properly
sb.Append(scriptString[i]); //Else, simply copy through
}
return sb.ToString();
}
void ProcessRandomCommand()
{
//Find the closing brace
int closingBrace = scriptString.IndexOf("}", i);
if (closingBrace == -1)
throw new Exception("Closing brace not found");
i++; //Makes the next statement nicer
string randomOptionsDeclaration = scriptString.SubString(i, closingBrace - i);
i = closingBrace; //Not closingBrace+1, because the caller will continue, and the for loop will then increment i
string[] randomOptions = randomOptionsDeclaration.Split(',');
int randomIndex = 0; //TODO: Randomisation here
sb.Append(randomOptions[randomIndex]);
}
}