 <p>If you do have control over a, one of the other answers is probably the best way to go around this.</p> <p>Assuming you have no control on the contents of string a, you could call a.split("order by"), save the two resulting pieces into two variables a1 and a2 and creating a new string with b between a1 and a2. Something like this (untested):</p> <pre><code>String[] temp = a.split("order by"); String result = temp[0] + " " + b + " order by " + temp[1]; </code></pre> <p>Again, I haven't really tested this, but you get the idea.</p> <p>On another note, it looks like you're doing some bare sql manipulation there. Don't do this, my friend. Your code will be pretty vulnerable to sql injection. Use prepared statements instead.</p>