📜  斯卡拉 |字符串插值

📅  最后修改于: 2022-05-13 01:54:57.925000             🧑  作者: Mango

斯卡拉 |字符串插值

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

  1. 字符串必须以起始字符定义为s / f / raw
  2. 字符串中的变量必须以“$”作为前缀。
  3. 表达式必须用大括号 ({, }) 括起来,并添加“$”作为前缀。

句法:

// x and y are defined
val str = s"Sum of $x and $y 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 and $y is ${x+y}"
      
            // with s interpolator
            val str2 = s"Sum of $x and $y is ${x+y}"
          
            println("str1: "+str1)
            println("str2: "+str2)
        }
    }
    

    输出:

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

    示例 2:函数调用

    // 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 and $y is ${add(x, y)}"
      
            // with s interpolator
            val str2 = s"Sum of $x and $y is ${add(x, y)}"
          
            println("str1: " + str1)
            println("str2: " + str2)
        }
    }
    

    输出:

    str1: Sum of $x and $y is ${add(x, y)}
    str2: Sum of 20 and 10 is 30
  2. 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 is $x%.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 is $x%04d"
          
            println(str1)
            println(str2)
        }
    }
    

    输出:

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

    如果我们在使用%d说明符完成格式化时尝试传递Double值,编译器会输出错误。在%f说明符的情况下,传递Int是可以接受的。

  3. 原始插值器:字符串字面量应以“原始”开头。此插值器将转义序列视为与字符串中的任何其他字符相同。
    示例:打印转义序列:
    // 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