← Back to Scala Course | Chapter 2: Variables & Types | Lesson 5 of 8

String क्या है

एक String text का एक टुकड़ा है, जैसे किसी ribbon पर एक शब्द या वाक्य। एक बार बन जाने पर इसे जगह पर edit नहीं किया जा सकता, इसलिए बदलाव एक नई string बनाते हैं।

In this page:

  1. String
Syntax
scala
val text: String = "some text"
text.length
text.toUpperCase

String

एक String characters का एक immutable sequence है, जिसे double quotes से लिखा जाता है। Strings + से concatenation, बहुत सारे built-in methods, और s"..." से interpolation (जिसे अपने अलग tutorial में कवर किया गया है) को support करते हैं।

चूंकि strings immutable हैं, हर "modification" वास्तव में एक नई string बनाती है।

Note: readability के लिए + concatenation के बजाय string interpolation (s"Hello $name") को प्राथमिकता दें।

उदाहरण: String

markup
object Main extends App {
  val first = "Ada"
  val last = "Lovelace"
  val full = first + " " + last

  println(full)
  println(full.length)
  println(full.toUpperCase)
}
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. s(0) = X जैसे किसी string को जगह पर बदलने की कोशिश करना, जबकि strings immutable हैं और आपको एक नई बनानी होगी।
  2. strings को eq से compare करना और content comparison की उम्मीद करना, जबकि == content compare करता है।
  3. यह भूल जाना कि code में full.length कोई parentheses नहीं लेता, और full.length() लिखना जहां style अलग है; दोनों काम करते हैं लेकिन Scala convention इन्हें छोड़ देता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.