Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala string template

Is there default(in SDK) scala support for string templating? Example: "$firstName $lastName"(named not numbered parameters) or even constructs like for/if. If there is no such default engine, what is the best scala library to accomplish this.

like image 997
yura Avatar asked Jun 04 '11 13:06

yura


2 Answers

If you want a templating engine, I suggest you have a look at scalate. If you just need string interpolation, "%s %s".format(firstName, lastName) is your friend.

like image 185
Kim Stebel Avatar answered Dec 09 '22 03:12

Kim Stebel


Complementing Kim's answer, note that Java's Formatter accepts positional parameters. For example:

"%2$s %1$s".format(firstName, lastName)

Also, there's the Enhanced Strings plugin, which allows one to embed arbitrary expressions on Strings. For example:

@EnhanceStrings // enhance strings in this scope
trait Example1 {
  val x = 5
  val str = "Inner string arithmetics: #{{ x * x + 12 }}"
}

See also this question for more answers, as this is really a close duplicate.

like image 35
Daniel C. Sobral Avatar answered Dec 09 '22 02:12

Daniel C. Sobral