← Back to Scala Course | Chapter 3: Operators & Expressions | Lesson 5 of 7

String को जोड़ना

String concatenation plus sign से text के टुकड़ों को जोड़ता है, जैसे train के डिब्बों को एक लंबी train में जोड़ना। Scala किसी number को text से भी जोड़ सकता है।

In this page:

  1. String Concatenation
Syntax
scala
val result = string1 + string2
val message = "text " + value

String Concatenation

+ operator strings को जोड़ता है, और जब एक side पहले से String हो तो Scala automatically non-string operands को उनके string form में convert कर देता है।

सरल मामलों के लिए + ठीक काम करता है, लेकिन कई embedded values के साथ strings बनाने के लिए आमतौर पर string interpolation को प्राथमिकता दी जाती है, क्योंकि यह ज़्यादा readable है। दोनों approaches आखिर में एक नई immutable String बनाते हैं।

Note: कुछ से ज़्यादा concatenated पीस के लिए, string interpolation (s"...") chained + operators से ज़्यादा साफ़ पढ़ी जाती है।

उदाहरण: String Concatenation

markup
object Main extends App {
  val first = "Hello"
  val second = "Scala"
  val combined = first + ", " + second + "!"

  println(combined)
  println("Count: " + 42)
}
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. पहले किसी number को concatenate करना, जैसे 1 + 2 + "a", जो 3a देता है, 12a नहीं।
  2. loop में + से एक लंबी string बनाना, जबकि StringBuilder ज़्यादा efficient है।
  3. + का उपयोग किसी null string के साथ करना, जिससे result में null text दिखाई देता है।
🔒

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.