Scala line pattern

Is scala support for string standardization supported by default (in the SDK)? Example: "$ firstName $ lastName" (called unnumbered parameters) or even constructs, for example for / if. If there is no such mechanism by default, then which of the best scala library is for this.

+6
source share
5 answers

In addition to Kim, answer that the Java Formatter accepts positional parameters. For instance:

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

In addition, there is an Enhanced Strings plugin that allows you to insert arbitrary expressions into strings. For instance:

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

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

+4
source

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

+9
source

In Scala 2.10 and later, you can use string interpolation

 val name = "James" println(s"Hello, $name") // Hello, James val height = 1.9d println(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall 
+2
source

This compiler plugin provided string interpolation for a while:

http://jrudolph.github.com/scala-enhanced-strings/Overview.scala.html

More recently, the function seems to be turning it into a scala trunk: https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/run/stringInterpolation.scala - which generates some interesting features: https: //gist.github.com/a69d8ffbfe9f42e65fbf (not sure if this is possible with the plugin, I doubt it).

+1
source

Use the very popular +" / "+ for templates, for example:

 firstname+" "+lastName 
-1
source

Source: https://habr.com/ru/post/889767/


All Articles