Is there an alternative to String.format (...)

You can use the String.format(...) method to compile an error, logging, or any other String messages. Unfortunately, this method is not type safe, so the next source will throw an IllegalFormatException

 String s = String.format("My message has %d characters !", "30"); 

Is there any other alternative for composing such messages besides the StringBuilder class.

My personal opinion is that a complex message becomes harder to read with an instance of StringBuilder.

+7
java string
source share
1 answer

Using String.format() with a single% s format specifier is effectively typeafe (an opaque type might be a better term) since each object implements the toString() method and even processes null objects.

Of course, you have little control over the actual format of the string unless you implement toString() ...

+7
source share

All Articles