Scala字符串插值深度

Before reading this post, please go through my previous Scala Language basic concepts. In this post, we are going to discuss about Scala’s String Interpolation concept with suitable examples. And also we will discuss similar kind of concept supported by Java Language.

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

发表简要目录: (Post Brief Table of Content:)

  • Introduction

    介绍
  • What is String Interpolation?

    什么是字符串插值?
  • String Interpolation Rules

    字符串插值规则
  • Types of String Interpolations In Scala

    Scala中的字符串插值类型
  • s String Interpolator

    字符串内插器
  • f String Interpolator

    f字符串插值器
  • raw String Interpolator

    原始字符串内插器
  • Compare Scala’s String Interpolation With Java

    将Scala的字符串插值与Java比较

介绍 (Introduction)

Scala Team has introduced String Interpolation concept in Scala 2.10 version to facilitate String processing in better way. As we know, Scala treats String a sequence of Characters.

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

This String Interpolation allows defining the following things in a String Literal to process them by the Scala Compiler:

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

  • Variables

    变数
  • Expressions

    表达方式
  • Method or Function Calls

    方法或函数调用
  • Object Fields

    对象字段

We will discuss each these concepts one by one in coming sections.

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

什么是字符串插值? (What is String Interpolation?)

In Scala, String Interpolation means that the replacement of defined variables or expressions in a given String with values.

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

String Interpolation is used to process String literals in easy way. To use this concept, We should follow some rules and syntax.

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

字符串插值规则: (String Interpolation Rules:)

In Scala, we should follow these rules to use String Interpolation concept to process String Literals. These rules are same for all kinds of String Interpolation.

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

  • Define Strings starting with s or f or raw letter

    定义以s或f或原始字母开头的字符串
  • Define variables in that String with $variable_name syntax

    使用$ variable_name语法在该字符串中定义变量
  • Define expressions in that String with ${expression} syntax

    使用$ {expression}语法在该字符串中定义表达式
  • Define Object fields with ${object.field} syntax

    使用$ {object.field}语法定义对象字段

Scala中的字符串插值类型 (Types of String Interpolations In Scala)

Scala supports mainly three kinds of String Interpolation:

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

  • s String Interpolator

    字符串内插器
  • f String Interpolator

    f字符串插值器
  • raw String Interpolator

    原始字符串内插器

Now we will take each kind of String Interpolator and discuss it with some examples in coming sections.

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

字符串内插器 (s String Interpolator)

In this String Interpolation, we should define String Literals with s String Interpolator. That means String Literal should start with ‘s’ letter.

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

We use this Interpolation concept to access Variables, Object Fields, Function calls etc.

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

Example-1:- How to access Variables in a String Literal

示例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)

Example-2:- How to access Object Fields in a String Literal

示例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

Example-3:- How to access Function or Method Calls in a String Literal

示例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

Example-4:- How to access Expressions in a String Literal

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

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

f字符串插值器 (f String Interpolator)

In this String Interpolation, we should define String Literals with f String Interpolator. That means String Literal should start with ‘f’ letter.

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

We use this Interpolation concept to format numbers in easy way.

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

Example-1:- How to format Numbers in a String Literal

示例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

Here we can observe that if we use s String Interpolator to format numbers, we don’t get expected result. In this use cases, we should use f String Interpolator only.

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

原始字符串内插器 (raw String Interpolator)

In this String Interpolation, we should define String Literals with raw String Interpolator. That means String Literal should start with ‘raw’.

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

It is used to accept escape sequence characters like ‘\n’ as it is in a String literal. That means when we don’t want to process or evaluate escape sequence characters then we should use raw String Interpolator.

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

Example-1:- How s String Interpolator processes Escape Sequence Characters in a String Literal

示例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

Example-2:- How raw String Interpolator works on Escape Sequence Characters in a String Literal

示例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比较 (Compare Scala’s String Interpolation With Java)

When we compare Scala’s String Interpolation With Java, Java does not support this concept. However, to to this kind of job for Strings, Java has a ‘printf’ function.

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

In Java, ‘printf’ function takes two kinds of parameters: first parameter is actual processed String and rest all are substituted variables or values.

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

Example:-

例:-

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

Output:-

输出:-

Player Details: Ram 95.50

Scala’s f String Interpolator is similar to Java’s printf function.

Scala的f String Interpolator与Java的printf函数相似。

That’s it all about “Scala String Interpolation”. We will discuss some more Scala Programming concepts in my coming posts.

这就是“ Scala字符串插值”的全部内容。 我们将在我的后续文章中讨论更多的Scala编程概念。

Please drop me a comment if you like my post or have any issues/suggestions.

如果您喜欢我的帖子或有任何问题/建议,请给我评论。

翻译自: https://www.journaldev.com/10326/scala-string-interpolation-in-depth

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值