Scala StringContext类

StringContext (StringContext)

The stringContext class in Scala is used to support string interpolation. The class extends the product with serializable. And has linear supertypes Serializable, java.io.Serializable, Product, Equals, AnyRef, Any.

Scala中的stringContext类用于支持字符串插值 。 该类通过可序列化扩展了产品。 并具有线性超类型Serializablejava.io.SerializableProductEqualsAnyRefAny

The class has the following members that support string interpolation:

该类具有以下支持字符串插值的成员:

  1. Formatted string interpolater: macro def f[A >: Any] (argument : A*) : String

    格式化的字符串插值器 :宏def f [A>:任何](参数:A *):字符串

  2. Raw string interpolater: def raw(argument: Any*) : String

    原始字符串插值器: def raw(参数:Any *):字符串

  3. Simple string interpolater: def s(argument: Any* ): String

    简单的字符串插值器: def s(参数:Any *):字符串

All three functions are used for string interpolation. And are used to add variables between the formatted strings.

所有这三个函数都用于字符串插值。 和用于在格式化的字符串之间添加变量。

Let's see each of the methods in action to interpolate strings.

让我们看一下每个插值字符串的方法。

格式化的字符串插值器 (Formatted string interpolater)

It is used to add the given arguments (variables/ expressions) in between the string. At the specified positions. 

它用于在字符串之间添加给定的参数(变量/表达式)。 在指定位置。

Syntax:

句法:

macro def f[A >: Any] (argument : A*) : String

Arguments: It can take any number of arguments that will be inserted in the string. The variable after $ is arguments.

参数:它可以采用将在字符串中插入的任意数量的参数。 $之后的变量是参数。

Return Type: It returns a string which is interpolated string.

返回类型:返回一个字符串,该字符串是插值字符串。

Example:

例:

object MyClass {
    def strPrint(proglang : String , year : Int){
        printf(f"$proglang was released in the year $year.\n")
    }
        
    def main(args: Array[String]) {
        strPrint("Scala", 2004)
        strPrint("JavaScript", 1995)
    }
}

Output:

输出:

Scala was released in the year 2004.
JavaScript was released in the year 1995.

In the above code, we have created a function strPrint() which accepts two arguments a string and an integer value. And interpolate them to a string in the following way:
proglang + " was released in the year " + year + "." and prints the result based on the arguments.

在上面的代码中,我们创建了一个函数strPrint() ,该函数接受两个参数:字符串和整数值。 并通过以下方式将它们插入到字符串中:
proglang +“发布于年份” + year +“。 并根据参数打印结果。

2)原始字符串内插器 (2) Raw string interpolator)

It is used to insert the given arguments (variables) in their places defined in the string.

它用于将给定的参数(变量)插入字符串中定义的位置。

Syntax:

句法:

def raw(argument: Any*) : String

Arguments: It can take any number of arguments that will be inserted in the string. The variable after $ is arguments.

参数:它可以采用将在字符串中插入的任意数量的参数。 $之后的变量是参数。

Return Type: It returns a string which is interpolated string.

返回类型:返回一个字符串,该字符串是插值字符串。

Example:

例:

object MyClass {
    def RawStrInterpolator(employee : String , salary : Int){
        println(StringContext(""," gets a salary of Rs.", ".").raw(employee , salary))
    }
        
    def main(args: Array[String]) {
        RawStrInterpolator("Sudhir", 175000)
        RawStrInterpolator("Ramesh", 50000)
    }
}

Output:

输出:

Sudhir gets a salary of Rs.175000.
Ramesh gets a salary of Rs.50000.

In the above code, we have created a method RawStrInterpolator() that demonstrates the functioning of Raw String Interpolator. The method accepts two arguments named employee and salary and interpolates then to the string using the raw method. The output of the interpolated string is of the form :
employee + " gets a salary of Rs." + salary + ".". And then prints the output based on the arguments.

在上面的代码中,我们创建了一个RawStrInterpolator()方法来演示Raw String Interpolator的功能。 该方法接受两个名为employee和salary的参数,然后使用raw方法对字符串进行插值。 插值字符串的输出格式为:
员工+“薪水为卢比”。 +薪水+“。 。 然后根据参数打印输出。

3)简单的字符串内插器 (3) Simple string interpolator)

This method is used to add the given arguments in their places in the given string.

此方法用于将给定参数添加到给定字符串中的位置。

Syntax:

句法:

def s(argument: Any*) : String

Arguments: It can take any number of arguments that will be inserted in the string. The variable after $ is arguments.

参数:它可以采用将在字符串中插入的任意数量的参数。 $之后的变量是参数。

Return Type: It returns a string which is interpolated string.

返回类型:返回一个字符串,该字符串是插值字符串。

Example:

例:

object MyClass {
    def simpleStrInterpolator(principal : Int , rate : Int){
        println(s"The interest on $principal at the rate of $rate for 1 year is ${(principal*rate*1)/100 }")
    }
        
    def main(args: Array[String]) {
        simpleStrInterpolator(175000, 8)
        simpleStrInterpolator(50000, 10)
    }
}

Output:

输出:

The interest on 175000 at the rate of 8 for 1 year is 14000
The interest on 50000 at the rate of 10 for 1 year is 5000

In the above code, we have created a function simpleStrInterpolator() which interpolates the string using s method. We have passed two arguments to the string which are principal and rate. The output of the interpolated string is of the form:
"The interest on" + principal + "at the rate of" + rate + "for 1 year is" + {(principal*rate*1)/100 }

在上面的代码中,我们创建了一个函数simpleStrInterpolator() ,该函数使用s方法对字符串进行插值。 我们向字符串传递了两个参数,分别是principal和rate。 插值字符串的输出格式为:
“利率” +“本金” +“利率” +“利率” +“一年”为“ + {(本金*利率* 1)/ 100}

The last expression will be solved and calculated value will be printed.

最后一个表达式将被求解并计算出计算值。

翻译自: https://www.includehelp.com/scala/stringcontext-class.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值