Search code examples
javastringbuffer

Insert string inside another string from one index to another index in between the string


I have a SQL select query already written in java. Now i want to select few more columns. In that case i need to write column names before the index of from keyword. Like this Initial query-

StringBuffer query;
query = " Select name,age,dob from employee"

New query should be like this-

query = "select name , age , dob,city from employee"

For this i tried query= query.insert(indexof(dob),"city");

But i think this will overwrite from. My question comes down to whether insert creates enough space to insert the string or it overwrites the earlier string? Please help.


Solution

  • instead of getting index value of dob, get index value of from

    query= query.insert(original.indexOf("from"),"city ");