Câu hỏi phỏng vấn Kotlin
Câu hỏi

How to correctly concatenate a String in Kotlin?

Câu trả lời

Here are a few ways to correctly concatenate strings in Kotlin:

String Interpolation

The most common and concise way to concatenate strings in Kotlin is using string interpolation with the $ symbol:

kotlin Copy
val name = "Alice"
val age = 25
println("My name is $name and I am $age years old.")

Output:

Copy
My name is Alice and I am 25 years old.

String Templates

You can also use string templates to insert expressions inside strings:

kotlin Copy
val x = 5
val y = 10
println("x + y = ${x + y}")

Output:

Copy
x + y = 15

Plus Operator

The + operator can be used to concatenate strings:

kotlin Copy
val firstName = "John"
val lastName = "Doe"
va...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

Why would you use apply in Kotlin?

junior

What is the idiomatic way to remove duplicate strings from array?

expert

When to use and do not use an inline function in Kotlin?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào