I was wondering what the best way to format a string would be in Scala. I'm reimplementing the toString method for a class, and it's a rather long and complex string. I thought about using String.format but it seems to have problems with Scala. Is there a native Scala function for doing this?
I was simply using it wrong. Correct usage is .format(parem1, parem2).
How about good old java.text.MessageFormat
?
The thing to watch out for with String#format
is the fact that it is actually implemented using reflection (as of v2.7.4). It delegates to the Java API, but the reflection adds a pretty significant overhead to a comparatively minor method call. You may want to consider Java-style string concatenation, just for performance reasons. As I understand it, Scala version 2.8.0 should resolve this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With