Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to format a string in Scala?

Tags:

java

string

scala

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?

like image 261
Rayne Avatar asked May 12 '09 23:05

Rayne


3 Answers

I was simply using it wrong. Correct usage is .format(parem1, parem2).

like image 180
Rayne Avatar answered Sep 19 '22 20:09

Rayne


How about good old java.text.MessageFormat?

like image 41
Apocalisp Avatar answered Sep 23 '22 20:09

Apocalisp


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.

like image 25
Daniel Spiewak Avatar answered Sep 23 '22 20:09

Daniel Spiewak