在 GitHub 上編輯此頁面

內插中的跳脫字元

在 Scala 2 中,沒有直接的方法可以在引號內插中表示雙引號字元 "(三引號內插除外)。無法使用 \ 字元,因為內插器本身會決定如何處理跳脫,因此剖析器不知道 " 字元應跳脫還是用作終止符。

在 Scala 3 中,我們可以使用內插的 $ 元字元來跳脫 " 字元。範例

val inventor = "Thomas Edison"
  val interpolation = s"as $inventor said: $"The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense.$""