当前位置:网站首页>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
边栏推荐
- (九)详解广播机制
- [JVM Series 5] performance testing tool
- Time processing class in PHP
- Application framework / capability blueprint
- 2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
- Simulink代码生成: 查表模块及其代码
- Pollution discharge fees of listed companies 2010-2020 & environmental disclosure level of heavy pollution industry - original data and calculation results
- Peking University HP financial index - matching enterprise green innovation index 2011-2020: enterprise name, year, industry classification and other multi indicator data
- Nuggets new oil: financial knowledge map data modeling and actual sharing
- Technical documentbookmark
猜你喜欢

Carbon neutralization & Patent Innovation: multi indicator data such as patent panels (original documents) of provinces, cities and counties, and the number of low-carbon patents authorized

Figure data * reconstruction subgraph

Summary of virtualization technology development

Azure SQL db/dw series (14) -- using query store (3) -- common scenarios

Pollution discharge fees of listed companies 2010-2020 & environmental disclosure level of heavy pollution industry - original data and calculation results

Brief introduction: distributed cap theory and base theory
![[JVM series 4] common JVM commands](/img/32/339bf8a2679ca37a285f345ab50f00.jpg)
[JVM series 4] common JVM commands

MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string

QML connecting to MySQL database
![[JVM Series 5] JVM tuning instance](/img/29/271fa25a338ee1268f7bce58673e11.jpg)
[JVM Series 5] JVM tuning instance
随机推荐
[azure data platform] ETL tool (1) -- Introduction to azure data factory
Quickly obtain the attributes of the sub graph root node
MASA Auth - SSO與Identity設計
How to write product requirements documents
2-year experience summary to tell you how to do a good job in project management
Spark Foundation
look on? What is the case between neo4j and ongdb?
Redis memory optimization and distributed locking
Mongodb distributed cluster deployment process
To resolve project conflicts, first-class project managers do so
C method parameter: in
C # simple understanding - static members and static classes
MySQL imports and exports multiple libraries at one time
YoloV5-Face+TensorRT:基于WIN10+TensorRT8.2+VS2019得部署
Transaction processing in PDO
[azure data platform] ETL tool (4) - azure data factory debug pipeline
测试写入mysql数据300W条,每次1.6-2W之间就断掉然后出现以下问题:
【同步功能】2.0.16-19 版本都有同步功能修复的更新,但未解决问题
Azure SQL db/dw series (13) -- using query store (2) -- report Introduction (2)
Use cypher to get the tree of the specified structure