1. 程式人生 > >【scala 格式化操作】Scala的字串 格式化 輸出

【scala 格式化操作】Scala的字串 格式化 輸出

主要內容:
1、字串通過+號連線(類似java)。
2、printf用法 (類似C語言)字串通過 % 傳值。
3、字串通過$引用(類似PHP)。

package DataStructureAndAlgorithm

/**
  * Created by Administrator on 2017/12/23.
  */

case class BookPrint(name: String, author: String)
object Test1ScalaPrint extends  App{
  val name = "ApacheCN"
  val age  = 1
  val url  = "www.apache.wiki"
val book = new BookPrint("www.baidu.com", "開心果汁") println("name=" + name, "age=" + age, "url=" + url, "author=" + book.author) printf("name=%s, age=%d, url=%s, author=%s\n", name, age, url, book.author) println(s"name=$name, age=$age, url=$url, author=${book.author}") }

執行結果:

(name=ApacheCN,age=1
,url=www.apache.wiki,author=開心果汁) name=ApacheCN, age=1, url=www.apache.wiki, author=開心果汁 name=ApacheCN, age=1, url=www.apache.wiki, author=開心果汁 Process finished with exit code 0