String interpolation in Scala?

后端 未结 5 1272
粉色の甜心
粉色の甜心 2021-01-04 04:43

I wanted to ask if there is any type of string interpolation in Scala. I have made a search on the subject but \'till now I have found that there is no string interpolation.

5条回答
  •  [愿得一人]
    2021-01-04 04:51

    You can do it C-style:

    "Interpolate my %s here" format List(1,2,3)
    
    //String = Interpolate my List(1, 2, 3) here
    

    or

    List(1,2,3) formatted "Interpolate my %s here"
    

    You can use these on anything with a toString (i.e. anything)

    case class Foo(n: Int)
    Foo(42) formatted "Here is a %s !!!!"
    //String = Here is a Foo(42) !!!!
    

    although the former is more flexible in terms of enabling multiple interpolations in a single string (since it can take multiple arguments).

提交回复
热议问题