当前位置:网站首页>Three ways of scala string interpolation
Three ways of scala string interpolation
2022-06-13 03:29:00 【TRX1024】
String interpolation : A mechanism for embedding variable references directly into the procedure string literal . Only in Scala-2.10 And later versions support .
1、's' String interpolator
written words 's' Allows you to use variables directly when processing strings . Anything in the scope String Variables can be used in strings .
usage 1:
stay println In the sentence String Variable ($name) Append to normal string (Hello) in :
val name = "TRX"
println(s"Hello, $name") //output: Hello, TRX
usage 2:
The string inserter can also handle arbitrary expressions . Use 's' The string inserter handles any expression with (${1 + 1}) String (1 + 1) The following code snippet for . Any expression can be embedded in ${} in .
println(s "1 + 1 = ${1 + 1}") //output: 1 + 1 = 2
2、'f' String interpolator
written words 'f' The interpolator allows you to create a formatted string , Be similar to C In language printf. In the use of 'f' Interpolator , All variable references should be printf Style format specifier , Such as %d,%i,%f etc. .
Let's take a general string as an example to append floating-point values (height = 1.9d) And string variables (name =“TRX”). The following code snippet implements 'f' Interpolator . here $name%s Print String Variable TRX and $height%2.2f Print floating point values 1.90.
val height = 1.9d
val name = "TRX"
println(f"$name%s is $height%2.2f meters tall") //TRX is 1.90 meters tall
3、'raw' String interpolator
'raw' The interpolator is similar to 's' Interpolator , But it does not perform literal escape within the string .
's'Interpolator for , The program code is as follows :
println(s"Result = \n a \n b")
// Output results -
Result =
a
b
'raw'Interpolator for , The program code is as follows -
println(raw"Result = \n a \n b")
// Output results -
Result = \n a \n b
边栏推荐
- C语言程序设计——从键盘任意输入一个字符串(可以包含:字母、数字、标点符号,以及空格字符),计算其实际字符个数并打印输出,即不使用字符串处理函数strlen()编程,但能实现strlen()的功能。
- Simulink代码生成: 查表模块及其代码
- Nuggets new oil: financial knowledge map data modeling and actual sharing
- Use of compact, extract and list functions in PHP
- Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade
- [figure data] how long does it take for the equity network to penetrate 1000 layers?
- Two Chinese vector map data with map review number
- Neo4j auradb free, the world's leading map database
- Array in PHP array function_ Slice and array_ flip
- Panel for measuring innovation efficiency of 31 provinces in China (using Malmquist method)
猜你喜欢
![[azure data platform] ETL tool (8) - ADF dataset and link service](/img/bf/d6d3a8c1139bb8d38ab9ee1ab9754e.jpg)
[azure data platform] ETL tool (8) - ADF dataset and link service

DTCC | 2021 China map database technology conference link sharing

YoloV5-Face+TensorRT:基于WIN10+TensorRT8.2+VS2019得部署

Simulink代码生成: 查表模块及其代码

MASA Auth - SSO与Identity设计

Nuggets new oil: financial knowledge map data modeling and actual sharing

The latest collation of the number of years of education per capita in the country and provinces -1989-2020- includes the annual original data, calculation process and result summary

2021-08-30 distributed cluster

The most complete ongdb and neo4j resource portal in history

On the career crisis of programmers at the age of 35
随机推荐
Configuration and practice of shardingsphere JDBC sub database separation of read and write
Data Governance Series 1: data governance framework [interpretation and analysis]
Summary of rust language practice
The most complete ongdb and neo4j resource portal in history
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
YoloV5-Face+TensorRT:基于WIN10+TensorRT8.2+VS2019得部署
ip地址及分类
Patrick Pichette, partner of inovia, former chief financial officer of Google and current chairman of twitter, joined the board of directors of neo4j
MMAP usage in golang
Sparksql of spark
Personal understanding of grammar sugar
Implode and explode in golang
MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables
Environmental pollution, enterprises, highways, fixed assets, foreign investment in all prefecture level cities in China - latest panel data
Explode and implode in PHP
[azure data platform] ETL tool (5) -- use azure data factory data stream to convert data
在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
Technical documentbookmark
Azure SQL db/dw series (14) -- using query store (3) -- common scenarios
Use of compact, extract and list functions in PHP