String क्या है
एक String text का एक टुकड़ा है, जैसे किसी ribbon पर एक शब्द या वाक्य। एक बार बन जाने पर इसे जगह पर edit नहीं किया जा सकता, इसलिए बदलाव एक नई string बनाते हैं।
In this page:
Syntax
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
object Main extends App {
val first = "Ada"
val last = "Lovelace"
val full = first + " " + last
println(full)
println(full.length)
println(full.toUpperCase)
}
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. #}
आम गलतियां
s(0) = Xजैसे किसी string को जगह पर बदलने की कोशिश करना, जबकि strings immutable हैं और आपको एक नई बनानी होगी।- strings को
eqसे compare करना और content comparison की उम्मीद करना, जबकि==content compare करता है। - यह भूल जाना कि 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: