I want to remove the last character of a Java string. The common solution is to do this:
String sql = "update table set field_one = :value1, field_two = :value2,";
sql = sql.substring(0, sql.length() - 1);
However, isn't this approach really inefficient? Java Strings are immutable, which means that the above substring operation would cause the JVM to allocate a new String that is only one character shorter than the old string was. Wouldn't this be really inefficient if you had a string that was thousands of characters long?
What is a more efficient way of removing the last character of a String in Java?