Question

I'm writing a MUD (text based game) at the moment using java. One of the major aspects of a MUD is formatting strings and sending it back to the user. How would this best be accomplished?

Say I wanted to send the following string:

You say to Someone "Hello!" - where "Someone", "say" and "Hello!" are all variables. Which would be best performance wise?

"You " + verb + " to " + user + " \"" + text + "\""

or

String.format("You %1$s to %2$s \"%3$s\"", verb, user, text)

or some other option?

I'm not sure which is going to be easier to use in the end (which is important because it'll be everywhere), but I'm thinking about it at this point because concatenating with +'s is getting a bit confusing with some of the bigger lines. I feel that using StringBuilder in this case will simply make it even less readable.

Any suggestion here?

Was it helpful?

Solution

If the strings are built using a single concatenation expression; e.g.

String s = "You " + verb + " to " + user + " \"" + text + "\"";

then this is more or less equivalent to the more long winded:

StringBuilder sb = new StringBuilder();
sb.append("You");
sb.append(verb);
sb.append(" to ");
sb.append(user);
sb.append(" \"");
sb.append(text );
sb.append('"');
String s = sb.toString();

(In fact, the Java compiler will compile the former into the latter ... almost.)

The efficiency issues arise when you start creating intermediate strings, or building strings using += and so on. At that point, StringBuilder becomes more efficient because you reduce the number of intermediate strings that get created and then thrown away.

Now when you use String.format(), it should be using a StringBuilder under the hood. However, format also has to parse the format String each time you make the call, and that is an overhead you don't have if you do the string building optimally.


Having said this, My Advice would be to write the code in the way that is most readable. Only worry about the most efficient way to build strings if profiling tells you that this is a real performance concern. (Right now, you are spending time thinking about ways to address a performance issue that may turn out to be insignificant or irrelevant.)

Another answer mentions that using a format string may simplify support for multiple languages. This is true, though there are limits as to what you can do with respect to such things as plurals, genders, and so on.

OTHER TIPS

Concateneting with plus, the compilet can transforms the code in performatic way. With string format i don t know.

I prefer cocatenation with plus, i think that is easer to undersand.

The key to keeping it simple is to never look at it. Here is what I mean:

Joiner join = Joiner.on(" ");

public void constructMessage(StringBuilder sb, Iterable<String> words) {
  join.appendTo(sb, words);
}

I'm using the Guava Joiner class to make readability a non-issue. What could be clearer than "join"? All the nasty bits regarding concatenation are nicely hidden away. By using Iterable, I can use this method with all sorts of data structures, Lists being the most obvious.

Here is an example of a call using a Guava ImmutableList (which is more efficient than a regular list, since any methods that modify the list just throw exceptions, and correctly represents the fact that constructMessage() cannot change the list of words, just consume it):

StringBuilder outputMessage = new StringBuilder();
constructMessage(outputMessage, 
         new ImmutableList.Builder<String>()
            .add("You", verb, "to", user, "\"", text, "\"")
            .build());

I will be honest and suggest that you take the first one if you want less typing, or the latter one if you are looking for a more C-style way of doing it.

I sat here for a minute or two pondering the idea of what could be a problem, but I think it comes down to how much you want to type.

Anyone else have an idea?

Assuming you are going to reuse base strings often Store your templates like

String mystring = "You $1 to $2 \"$3\""

Then just get a copy and do a replace $X with what you want.

This would work really well for a resource file too.

I think that concatenation with + is more readable than using String.format.

String.format is good when you need to format number and dates.

I think String.format looks cleaner. However you can use StringBuilder and use append function to create the string you want

The best, performance-wise, would probably be to use a StringBuffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top