String Templates क्या हैं
In this page:
val text = "Hello $variable"
val text = "Result: ${expression}"
val text = "Value: ${object.property}"
Simple Variable Templates
किसी string के अंदर variable name से ठीक पहले एक dollar sign रखना, जैसे $name, बिना concatenation की ज़रूरत के उस variable की current value को string में डाल देता है।
उदाहरण: Simple Variable Templates
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `name`, set to `"Kotlin"`
val name = "Kotlin"
// Declare a read-only variable `version`, set to `2`
val version = 2
// Print "Language: $name, Version: $version" to the console, with a trailing newline
println("Language: $name, Version: $version")
}
Login to try C/C++/Java/PHP code in the editor
Expression Templates with Curly Braces
एक साधारण variable name से ज़्यादा किसी भी चीज़ के लिए -- property access, function calls, या arithmetic -- expression को ${ } में wrap करें ताकि Kotlin इसे evaluate करे और result डाले।
उदाहरण: Expression Templates with Curly Braces
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `a`, set to `5`
val a = 5
// Declare a read-only variable `b`, set to `3`
val b = 3
// Print "Sum: ${a + b}, Product: ${a * b}" to the console, with a trailing newline
println("Sum: ${a + b}, Product: ${a * b}")
}
Login to try C/C++/Java/PHP code in the editor
Templates with Object Properties
String templates ${ } के अंदर सीधे किसी object की properties तक पहुंच सकते हैं, जो कई concatenations से string बनाने से कहीं ज़्यादा साफ़ है।
उदाहरण: Templates with Object Properties
// Define a data class `Point`
data class Point(val x: Int, val y: Int)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `p`, set to `Point(3, 4)`
val p = Point(3, 4)
// Print "Point is at (${p.x}, ${p.y})" to the console, with a trailing newline
println("Point is at (${p.x}, ${p.y})")
}
Login to try C/C++/Java/PHP code in the editor
Escaping a Literal Dollar Sign
जब किसी string को एक template शुरू करने के बजाय एक असली $ character चाहिए, इसे backslash से \$ के रूप में escape करें।
उदाहरण: Escaping a Literal Dollar Sign
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `price`, set to `20`
val price = 20
// Print "Price: \$$price" to the console, with a trailing newline
println("Price: \$$price")
}
Login to try C/C++/Java/PHP code in the editor
- किसी single variable से ज़्यादा जटिल expression के आस-पास curly braces भूल जाना, जैसे
${user.name}के बजाय$user.name। - ज़्यादा readable
$variablesyntax के बजाय आदत से हर जगह+से string concatenation का उपयोग करने की कोशिश करना। - जब किसी literal dollar sign को template शुरू नहीं करना चाहिए तब उसे
\$से escape करना भूल जाना।
- किसी string के अंदर
$variableNameउस variable की value को सीधे डालता है। - curly braces के साथ
${expression}किसी simple variable name से आगे की किसी भी चीज़ के लिए ज़रूरी है, जैसे कोई property access या calculation। - किसी string में एक literal dollar sign को template के रूप में treat होने से बचाने के लिए
\$के रूप में escape होना चाहिए। - String templates single-line और triple-quoted दोनों तरह की strings में काम करते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: