Better String formatting in Scala

前端 未结 7 1287
孤独总比滥情好
孤独总比滥情好 2020-12-02 12:03

With too many arguments, String.format easily gets too confusing. Is there a more powerful way to format a String. Like so:

\"This is #{number}          


        
7条回答
  •  旧巷少年郎
    2020-12-02 12:29

    In Scala 2.10 you can use string interpolation.

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

提交回复
热议问题