Generate word combination array in c#

前端 未结 3 684
闹比i
闹比i 2021-01-13 06:37

I have a string such as \"big bad dog\", how can I get an string[] array which includes all the possible word/phrase combinations?

So, I would like to return \"big\"

3条回答
  •  忘掉有多难
    2021-01-13 07:26

    What about splitting the string into array of separate words

    string str = "big fat dog";
    string[] words = str.Split(new Char[] { ' ', ',', '.', ':', '\t' });
    

    and then you can use this to make word combinations

    string[] words = new string[]{"big", "bad", "dog"}; 
    for(int mask = 0; mask < 1 << (words.Length); mask++) 
    { 
      string permutation = ""; 
      for(int i = 0; i < words.Length;  i++) 
      { 
        if((mask & (1 << (words.Length - 1 - i))) != 0) 
        { 
          permutation += words[i] + " "; 
        } 
      } 
      Console.WriteLine(permutation); 
    }
    

    I think regular expresion has no use here.

提交回复
热议问题