title: Getting a String between 2 words
body: <p>I want to get the string between 2 words in a string. like so:</p>
  
  <pre><code>String s = "Play A ROCK song"
  </code></pre>
  
  <p>So I want to get the string between: A and song, which in this case is rock.</p>
  
  <p>I tried this:</p>
  
  <pre><code>    public static String subStringBetween(String text, String after, String before) {
      String p1 = getWordsAfter(text, after);
      String p2 = getWordsBefore(text, before);
  
      Set&lt;String&gt; aSet = new HashSet&lt;&gt;(Arrays.asList(p1.split(" ")));
      Set&lt;String&gt; bSet = new HashSet&lt;&gt;(Arrays.asList(p2.split(" ")));
  
      Set&lt;String&gt; result = new HashSet&lt;&gt;(aSet);
      result.retainAll( bSet);
      String string = "";
      for (String s : result) {
          if (string == "") {
              string = s;
          }else{
              string +=" " + s;
          }
      }
      return string;
  }
  
  public static String getWordsAfter(String text, String word) {
      String[] splt = text.split(word);
      return splt[1];
  }
  
  public static String getWordsBefore(String text, String word) {
      String[] splt = text.split(word);
      return splt[0];
  }
  </code></pre>
  
  <p>It works, but If the string between A and Song is more than one word, it works, but prints something weird, like if it was: Play a Nice Rock Song
  the return would be Rock Nice, not Nice Rock</p>