String को जोड़ना
String concatenation plus sign से text के टुकड़ों को जोड़ता है, जैसे train के डिब्बों को एक लंबी train में जोड़ना। Scala किसी number को text से भी जोड़ सकता है।
In this page:
Syntax
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
object Main extends App {
val first = "Hello"
val second = "Scala"
val combined = first + ", " + second + "!"
println(combined)
println("Count: " + 42)
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- पहले किसी number को concatenate करना, जैसे
1 + 2 + "a", जो3aदेता है,12aनहीं। - loop में
+से एक लंबी string बनाना, जबकिStringBuilderज़्यादा efficient है। +का उपयोग किसीnullstring के साथ करना, जिससे result मेंnulltext दिखाई देता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: