[Offtopic] Concatenating Strings Best Practice

  • Hi fellow plugin dudes (and game devs),


    Bit of Friday night code optimization chat.


    Now on my blog, around three years ago I wrote somthing that goes a little like:


    "So I’ve always done it with the plus (+) operator turns out using a prefix $ is much more efficient in terms of memory usage."

    Java
    //less efficient
    string newString = firstString + " said hello";
    //more efficient
    string newString = $"{firstString} said hello";


    Disclaimer: honesty I can't actually remember posting this.


    So I just tried it (as I still just use the plus operator) as below:

    Java
    String firstString = "foo";
    string newString = $"{firstString} bar";


    But I just get an error message. " ; expected "


    I think, the problem is that this was wrongfully filed under Java (which I'm trying to compile it as), when in fact it may very well be C# code.


    First off, am I right that this is C#?


    Second, and more importantly, what do you think is the most optimal way of string concatination? Using the plus operator or using the String classes concat operator?


    Answers about it being negligable are not accepted. :crazy:


    paulevs I'm interested in knowing your thoughts on this ;)

  • This is indeed C# code (although a few other languages support that feature as well) :D It's called String interpolation. Java doesn't have such a feature directly... instead you either have to use regular concatenation, or the String.format() method. In newer Java versions there is also a new "formatted" method (e.g. "%s bar".formatted("foo");, which is the same as String.format("%s bar", "foo");).


    Strictly speaking, String concatenation is usually a lot faster than String.format(), but only the latter provides proper formatting options. But we're typically talking about nanoseconds here (so you wouldn't really notice a difference unless doing thousands or hundreds of thousands of calls) ^^


    So if you want formatted Strings, use "String.format()" (or the new ".formatted()" method), and if you just want to combine some Strings, use concatenation - unless you're in a loop, then it's much better to use a StringBuilder object directly:

    Java: Bad code
    String text = "Numbers: ";
    for(int i = 0; i < 100; i++){
    text += " " + i;
    }


    Java: Good code
    StringBuilder sb = new StringBuilder(300);
    sb.append("Numbers: ");
    for(int i = 0; i < 100; i++){
    sb.append(' ').append(i);
    }
    String text = sb.toString();
  • paulevs I'm interested in knowing your thoughts on this ;)

    As Red mentioned above - it is not Java code since java don't have such operators for strings. And yes - the most effective code to merge multiple strings will use StringBuilder. Old java converted string merging directly into StringBuilder, and newer have some other operators.

  • Am I correct to believe that it's always going to be better to use the StringBuilder class over String?

    You need to use it when you need to join more than 2 strings (for example when you need to merge 10 different messages), in other cases you can use normal string operations

  • Am I correct to believe that it's always going to be better to use the StringBuilder class over String?

    The Java compiler already turns a + concatenation either into a StringBuilder, or uses internal String magic (since Java 9) to combine them (which is even faster than a StringBuilder) ^^ So it's totally fine to use the + operator in most cases.

    But there are situations where the compiler typically can't optimize it, e.g. in loops or if the concatenation happens over multiple methods - so it's much better and faster to use a StringBuilder there ;)

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!