一般來說,使用的indexOf
很多方法可以做到這一點,但是你真的需要使用的indexOf。
int openBrace = originalString.indexOf("{");
if openBrace is -1,then not found。如果您不想通過所有大括號進行處理,那麼您將需要使用其他參數選項。
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);
}
(在有可能的錯誤,所以讓我知道如果你需要幫助改進它)
我希望你的想法。您當前使用拆分和包含的過程非常有限。
建議腳本解析架構
但是,出於性能,和你的整個處理腳本的系統的簡單,你可能要一次處理的originalString一個字符,並輸出到一個StringBuilder。
一般來說,使用的indexOf
很多方法可以做到這一點,但是你真的需要使用的indexOf。
int openBrace = originalString.indexOf("{");
if openBrace is -1,then not found。如果您不想通過所有大括號進行處理,那麼您將需要使用其他參數選項。
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);
}
(在有可能的錯誤,所以讓我知道如果你需要幫助改進它)
我希望你的想法。您當前使用拆分和包含的過程非常有限。
建議腳本解析架構
但是,出於性能,和你的整個處理腳本的系統的簡單,你可能要一次處理的originalString一個字符,並輸出到一個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]);
}
}
你爲什麼不復制粘貼在這裏你的代碼,而不是製作截圖? –