1

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?

teuber789
  • 1,527
  • 16
  • 28
  • 1
    Well, any approach you take will lead to creation of at least one new String (unless you use reflection to break immutability of the original string, which shouldn't be done). I suggest using `substring` instead of `replace` because it is more efficient and you are already doing it. You might also want to look at http://stackoverflow.com/questions/27949213/string-deduplication-feature-of-java-8 – TheLostMind May 26 '16 at 16:39
  • 1
    I don't think you can do any better. No matter how - you will have to create a new String as they're immutable. Just for the fun I wrote a little test programm using `substring` and `str.replaceAll(".$", "")` (regex), the Regex took nearly 17 times as long as the `substring` method... – Tobias Brösamle May 26 '16 at 17:05
  • As you said they are immutable, so no matter what it would have to allocate new memory for the new string. The alternative would need to have a mutablestring class to manipulate the underlying data, but just about everything takes String, which to convert would still require you to allocate the new memory. Plus you'd also need to write the mutablestringclass. The immutability of a string makes strings much more secure since the length is always known (compared to say, C) – Mgamerz May 26 '16 at 17:09
  • 1
    @TheLostMind you can't do anything useful here using reflection. The latest String impl uses its whole backing `char[]` (the `offset` and `length` fields were removed in java 8), and you can't change the length of an array using reflection. There is no getting around having to copy all but one chars into a new array, which is an O(n) operation – Bohemian May 26 '16 at 17:56
  • There is no more efficient way. – Louis Wasserman May 26 '16 at 18:11
  • @Bohemian - I was trying to say that he could use reflection to replace the last character with an `\u0000` in the same String. That would change the same array right? – TheLostMind May 26 '16 at 18:28
  • Wouldn't the string length still return the size of the original char array? That could lead to security issues. Otherwise it would have to scan the entire array everytime. – Mgamerz May 26 '16 at 18:29
  • @TheLostMind Java ain't C. Java doesn't use null terminated strings. The nul char is a valid character in a Java string. – Bohemian May 26 '16 at 19:52
  • @Bohemian - Ah. I got what you meant. Thanks :) – TheLostMind May 27 '16 at 05:21

0 Answers0