← Back to Kotlin Course | Chapter 2: Variables & Types | Lesson 4 of 7

String Templates क्या हैं

String templates आपको किसी variable की value को text के किसी टुकड़े के अंदर सीधे डालने देते हैं बस उसके सामने एक dollar sign लगाकर।
Syntax
kotlin
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

markup
// 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")
}

Expression Templates with Curly Braces

एक साधारण variable name से ज़्यादा किसी भी चीज़ के लिए -- property access, function calls, या arithmetic -- expression को ${ } में wrap करें ताकि Kotlin इसे evaluate करे और result डाले।

उदाहरण: Expression Templates with Curly Braces

markup
// 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}")
}

Templates with Object Properties

String templates ${ } के अंदर सीधे किसी object की properties तक पहुंच सकते हैं, जो कई concatenations से string बनाने से कहीं ज़्यादा साफ़ है।

उदाहरण: Templates with Object Properties

markup
// 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})")
}

Escaping a Literal Dollar Sign

जब किसी string को एक template शुरू करने के बजाय एक असली $ character चाहिए, इसे backslash से \$ के रूप में escape करें।

उदाहरण: Escaping a Literal Dollar Sign

markup
// 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")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी single variable से ज़्यादा जटिल expression के आस-पास curly braces भूल जाना, जैसे ${user.name} के बजाय $user.name।
  2. ज़्यादा readable $variable syntax के बजाय आदत से हर जगह + से string concatenation का उपयोग करने की कोशिश करना।
  3. जब किसी 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.