title: Doing Java String replacement efficiently
body: <p>We have the following code :</p>
  
  <pre><code>String templateQuery = "select * from my_table where col1=$1 or col2 like '%$2.$1'";
  String tmp = templateQuery;
  
  for(int i=1;i&lt;=maxCols;i++) {
      tmp = tmp.replaceAll("\\$"+i, data[i-1]);
  }
  </code></pre>
  
  <p>This code works fine as <code>maxCols</code> never exceeds 10. But my colleague disagree with me stating that this code consumes too much memory. Can you help us ?</p>
  
  <p><strong>EDIT</strong>:
  I have change the initial templateQuery with a much realistic one. Secondly, <code>templateQuery</code> can potentially be a <em>big</em> string.</p>
  
  <p><strong>EDIT 2</strong>:
  Thanks for those who have pointed out the <em>SQLInjection</em> problem.</p>