Developer/Java, Kotlin
[Scala] String Interpolation
데브포유
2017. 5. 22. 15:14
반응형
Scala 2.10.0에 추가된 String Interpolation 예제 입니다.
val name = "moonsun"
val msg = s"My name is $name"
println(msg)
==> My name is moonsun
문자열 변수를 $연산자로 문자열 내부에서 바로 사용할 수 있는 기능임.
"s"를 String Interpolator라고 함.
Starting in Scala 2.10.0, Scala offers a new mechanism to create strings from your data: String Interpolation.
String Interpolation allows users to embed variable references directly in processed string literals
printf 처럼 사용할 수 있는 f Interpolator도 있음
The f
Interpolator
Prepending f
to any string literal allows the creation of simple formatted strings, similar to printf
in other languages. When using the f
interpolator, all variable references should be followed by a printf
-style format string, like %d
. Let’s look at an example:
val height = 1.9d
val name = "James"
println(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall
반응형