2013-10-25 127 views
2

我的意思是在我的問題中,我試圖創建一個基本的腳本程序。我希望它從選擇的輸入單詞中選擇一個隨機單詞。獲取特殊字符內的項目

IE:

Hello {you,uyo,u}, whats up? 

它就會隨機抽取者之一在括號中選擇輸出類似:

Hello u, whats up?/Hello you, whats up?/Hello uyo, whats up? 

繼承人什麼伊夫試圖迄今: Image

請原諒任何不好的代碼,這是我第一次嘗試腳本。 程序是這樣的: Second img

該項目工程迄今,但我不能把別的消息中,但搶{},我想就能夠拋出{}任何地方,它會隨機選擇。

+4

你爲什麼不復制粘貼在這裏你的代碼,而不是製作截圖? –

回答

4

你可能要考慮加時賽正則表達式:

using System; 
using System.Linq; 
using System.Text.RegularExpressions; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var input = "Hello {you,uyo,u}, whats {up,down}?"; 
     var random = new Random(); 
     var result = Regex.Replace(input, @"{.*?}", m => 
     { 

      var values = m.Value.Substring(1, m.Value.Length - 2).Split(','); 
      return values.ElementAt(random.Next(values.Length)); 
     }); 

     Console.WriteLine(result); 
    } 
} 

不過要小心有關嵌入式{{}}。如果你期待的場景發生,你可能想:

  1. 寫自己的解析引擎,
  2. 或者使用Balancing Group Definitions
+1

這完全是我需要的,這樣的小代碼,非常感謝你! –

0

你想要這樣的事嗎?

string myString = "Hello {you,uyo,u}, whats up?"; 

string[] stringList = myString.Split('{'); 
string before = stringList[0]; 

stringList = stringList[1].Split('}'); 
string after = stringList[1]; 

stringList = stringList[0].Split(','); 



foreach (string s in stringList) 
{ 
    Console.WriteLine(before+" "+s+""+after); 
} 

Console.ReadLine(); 

輸出:

Hello you, whats up? 
Hello uyo, whats up? 
Hello u, whats up? 

爲了使它只選擇隨機字:

Random rand = new Random(); 

int i = rand.Next(stringList.Length); 

Console.WriteLine(before + " " + stringList[i] + "" + after); 

此解決方案假定您可以把耳只一次輸入字符串。但是你可以很容易地編輯/改進它來處理更多。例如,您可以首先計算字符串中的括號,並根據它創建一個foreach來檢索所有可能的值。

0

一般來說,使用的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]); 
    } 
}