Scala 字符串插值

Scala 字符串插值

字符串插值 是指在一个给定的字符串中用尊重的值来替换定义的变量或表达式。字符串插值提供了一种处理字符串字面的简单方法。要应用Scala的这一特性,我们必须遵循一些规则。

  1. String必须以 s / f / raw 为起始字符进行定义 。
  2. 字符串中的变量必须有’$’作为前缀。
  3. 表达式必须包含在大括号({, })中,并且’$’被添加为前缀。

语法

// x and y are defined
val str = s"Sum of x andy is ${x+y}"

**字符串插值器的类型 **

  1. s插值器:在字符串中,我们可以访问变量、对象字段、函数调用等。

示例1:变量和表达式。

// Scala program
// for s interpolator
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    {
      
        val x = 20
        val y = 10
      
        // without s interpolator
        val str1 = "Sum of x andy is {x+y}"
  
        // with s interpolator
        val str2 = s"Sum ofx and y is{x+y}"
      
        println("str1: "+str1)
        println("str2: "+str2)
    }
}

输出:

str1: Sum of x andy is ${x+y}
str2: Sum of 20 and 10 is 30

示例 2: function call

// Scala program
// for s interpolator
  
// Creating object
object GFG
{ 
    // adding two numbers
    def add(a:Int, b:Int):Int
    =
    { 
        a+b 
    }
      
    // Main method
    def main(args:Array[String])
    {
      
        val x = 20
        val y = 10
      
        // without s interpolator
        val str1 = "Sum of x andy is {add(x, y)}"
  
        // with s interpolator
        val str2 = s"Sum ofx and y is{add(x, y)}"
      
        println("str1: " + str1)
        println("str2: " + str2)
    }
}

输出:

str1: Sum of x andy is ${add(x, y)}
str2: Sum of 20 and 10 is 30
  1. f插值器:该插值器有助于轻松实现数字格式化。

要了解格式指定器如何工作,请参考格式指定器。

示例1:打印到小数点后2位。

// Scala program
// for f interpolator
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    {
      
        val x = 20.6
      
        // without f interpolator
        val str1 = "Value of x is x%.2f"
  
        // with f interpolator
        val str2 = f"Value of x isx%.2f"
      
        println("str1: " + str1)
        println("str2: " + str2)
      
    }
}

输出:

str1: Value of x is $x%.2f
str2: Value of x is 20.60

示例 2: 以整数形式设置宽度。

// Scala program
// for f interpolator
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    {
      
        val x = 11
      
        // without f interpolator
        val str1 = "Value of x is x%04d"
  
        // with f interpolator
        val str2 = f"Value of x isx%04d"
      
        println(str1)
        println(str2)
    }
}

输出:

Value of x is $x%04d
Value of x is 0011

如果我们试图传递一个Double值,而使用%d指定器进行格式化,编译器会输出一个错误。在使用%f指定器的情况下,传递Int是可以接受的。

  1. raw插值器: 字符串应该以’raw’开头。这个插值器对转义序列的处理与String中的任何其他字符相同。
    示例 :打印转义序列。
// Scala program
// for raw interpolator
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    {
          
        // without raw interpolator
        val str1 = "Hello\nWorld"
  
        // with raw interpolator
        val str2 = raw"Hello\nWorld"
      
        println("str1: " + str1)
        println("str2: " + str2)
    }
}

输出:

str1: Hello
World
str2: Hello\nWorld

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程