Scala字符串插值

时间:2020-02-23 14:41:49  来源:igfitidea点击:

在阅读本文之前,请仔细阅读我以前的Scala语言基本概念。
在本文中,我们将通过适当的示例讨论Scala的String Interpolation概念。
我们还将讨论Java语言支持的类似概念。

介绍

Scala团队在Scala 2.10版本中引入了String Interpolation概念,以更好的方式促进String处理。
众所周知,Scala将String视为字符序列。

此字符串插值允许在字符串文字中定义以下内容,以由Scala编译器进行处理:

  • 变数
  • 表达方式
  • 方法或者函数调用
  • 对象字段

我们将在接下来的部分中逐一讨论这些概念。

什么是字符串插值?

在Scala中,字符串插值表示用值替换给定字符串中定义的变量或者表达式。

字符串插值用于以简单的方式处理字符串文字。
要使用此概念,我们应遵循一些规则和语法。

字符串插值规则:

在Scala中,我们应遵循以下规则,以使用字符串插值概念处理字符串文字。
这些规则对于所有类型的字符串插值都是相同的。

  • 定义以s或者f或者原始字母开头的字符串
  • 使用$variable_name语法在该字符串中定义变量
  • 使用${expression}语法在该字符串中定义表达式
  • 使用${object.field}语法定义对象字段

Scala中的字符串插值类型

Scala主要支持三种字符串插值:

  • 字符串内插器
  • f字符串插值器
  • 原始字符串内插器

现在,我们将使用每种String Interpolator,并在接下来的部分中通过一些示例进行讨论。

s字符串插值器

在此字符串插值中,我们应使用s字符串插值器定义字符串文字。
这意味着String Literal应该以" s"字母开头。

我们使用这种插值概念来访问变量,对象字段,函数调用等。

示例1:-如何访问字符串文字中的变量

scala>val title1 = "Play"
title1: String = Play

scala>val title2 = "Scala"
title2: String = Scala

scala>val book1 = "Book($title1 2 for $title2)"
book1: String = Book($title1 2 for $title2)

scala>val book2 = s"Book($title1 2 for $title2)"
book2: String = Book(Play 2 for Scala)

示例2:-如何访问字符串文字中的对象字段

scala> class Person(val firstName: String, val lastName:String)
defined class Person

scala> val person1 = new Person("Ram","Posa")
person1: Person = Person@1bc53649

scala> val str1 = s"Person fullname: ${person1.firstName}-${person1.lastName}"
str1: String = Person fullname: Ram-Posa

示例3:-如何访问字符串文字中的函数或者方法调用

scala> class Person(val firstName: String, val lastName:String) {
   |   def fullName = firstName + "-" + lastName
   | }
defined class Person

scala> val person1 = new Person("Ram","Posa")
person1: Person = Person@1bc53649

scala> val str2 = s"Person fullname: ${person1.fullName}"
str2: String = Person fullname: Ram-Posa

示例4:-如何访问字符串文字中的表达式

scala> val add10and20 = s"Addition of 10 and 20 = ${10 + 20}"
add10and20: String = Addition of 10 and 20 = 30

f字符串插值器

在此字符串插值中,我们应使用f字符串插值器定义字符串文字。
这意味着String Literal应该以" f"字母开头。

我们使用此插值概念以简单的方式设置数字格式。

示例1:-如何格式化字符串文字中的数字

scala> val itemPrice = 10.5
itemPrice: Double = 10.5

scala> val str = s"Item Price : $itemPrice"
str: String = Item Price : 10.5

scala> val str = s"Item Price : $itemPrice%.2f"
str: String = Item Price : 10.5%.2f

scala> val str = f"Item Price : $itemPrice%.2f"
str: String = Item Price : 10.50

在这里我们可以看到,如果使用s字符串插值器格式化数字,则不会得到预期的结果。
在这种使用情况下,我们应该仅使用f String Interpolator。

原始字符串内插器

在此字符串插值中,我们应使用原始的字符串插值器定义字符串文字。
这意味着String Literal应该以" raw"开头。

它用于接受字符串文字中的转义字符,例如" \ n"。
这意味着当我们不想处理或者评估转义序列字符时,应使用原始的String Interpolator。

示例1:-字符串内插器如何处理字符串文字中的转义字符

scala> val itemName = "Laptop"
itemName: String = Laptop

scala> val itemPrice = 499.99
itemPrice: Double = 499.99

scala> val str = s"Item Details: \n Name: $itemName \t Price : $itemPrice%.2f"
str: String =
Item Details:
 Name: Laptop    Price : 499.99%.2f

示例2:-原始字符串插值器如何在字符串文字中的转义序列字符上工作

scala> val itemName = "Laptop"
itemName: String = Laptop

scala> val itemPrice = 499.99
itemPrice: Double = 499.99

scala> val str = raw"Item Details: \n Name: $itemName \t Price : $itemPrice%.2f"
str: String = Item Details: \n Name: Laptop \t Price : 499.99%.2f

比较Scala的字符串插值与Java

当我们将Scala的String Interpolation与Java比较时,Java不支持此概念。
但是,对于此类Strings工作,Java具有" printf"功能。

在Java中," printf"函数采用两种参数:第一个参数是实际处理过的String,其余的都是替换变量或者值。

例:

String playerName = "Ram";
float score = 95.5;
System.out.printf("Player Details: %s %d.2f", playerName, score);

输出:

Player Details: Ram 95.50

Scala的f字符串插值器类似于Java的printf函数。