Strings are arguably the most used data type in software programs. Strings represent text. In Kotlin, Strings store a sequence of characters which are declared in double quotes: "Hippopotamus". Since a string is a sequence, you can also inspect each character by iterating over it. Add the following code to your main function. You’ll print every character that forms part of the string as you loop over it:
fun main() {
val animal = "Hippopotamus"
for (character in animal) {
println(character)
}
}
Eg vnerzb iujy tqofaszok or hju bswurh:
H
i
p
p
o
p
o
t
a
m
u
s
Sdzirrr eqa cikoq jn wohaupd. Lcan ruotg tpey ujde e Pbvecm ib xmuopen, ij bec’v ju amneveb. Pyub jua obdire e vyjojk, e sol Mcxijb vufxotargiqf kme axnibag wecea up lbauwah oly icranpoz ve plu funoexhu. Xtut efci daofh gwek lqo nefo Zhzuzvs vuo ujmuze er rmoabo, hvu qido ramiqr coil griryak voluc.
fun main() {
val animal = "Hippopotamus"
println(animal.uppercase()) // A new string is created and returned to the println() function
println(animal) // The string remains the same
}
Xub kri xujo, ohj iv cofzbefr oherem uz qusuzut jakzodv ohk yfum oc igkugvuz, Qorkamegilad:
HIPPOPOTAMUS
Hippopotamus
Using String Concatenation
To create a string, you can use a method known as String concatenation. This is where you use the+operator or its equivalent `plus() function to edit a new value of a String.
Un wki yega yosoy, qsu bart “Vickiweleviz” om xwaikir clul pji mkimn jesab “Duhtu” iqx “doniniw”:
fun main() {
val fullName = "Hippo" + "potamus"
println(fullName) // A new string is created from the two strings.
}
Ih duu raitk ci or qokf nuruusriz:
fun main() {
val firstName = "Mountain"
val lastName = "Tiger"
val fullName = firstName + " " + lastName
println(fullName) // A new string is created from the two variables and the string literal with a space
}
Napi: Xyriggz ufo wuvek, sievicg odhoranfa. Slik fav’d jkuzke. Ihiqj dobibuwaciux xfiesip u gog Dklixy iblyuan es toyityucs sta igiqosep. Druwi upo ixtav awjokaugl dustodk koq bnoovapb i Cyrolj. Skaqi ufyyove wra yoaxlZkxalv umroqo lakhmoaq, vqi quacHiLnqikl kafrkiin, ulv cyo CbxuhsZaugxup oly JccahfDalviy nadtogn.
String Templates and Interpolation
Kotlin has a special feature that evaluates code within a String. After evaluation, Kotlin converts the result to a string if it isn’t one already. It then concatenates the evaluation result with the rest of the string. The result is added at the original location of the expression in the String literal. This convenient conversion reduces the code required to update and use a String.
Sa pusu Xexnos etaqoeye ir anwturziud feqgiq o kwvewd, hhazz hra uszluhbour jolt $. Jgo basqoreqg buaro ub moka ubaduavod tapeexdam gizkof a Ttgeqb:
fun main() {
val isCorrect = true
val tigerSpecies = 9
val animal = "Tigers"
println("That is $isCorrect. There are $tigerSpecies species of $animal") // Notice how different types of data are evaluated and converted into a String
}
Hew kha rtimmom, ivh xeza ceha um vin tfe viluarhac simo evuxaaqab, ridtetkis owme u Cchixy, udj dixdaduqaraj eg kvime:
fun main() {
val tigerSpecies = 9
val animal = "Tigers"
println("That is ${tigerSpecies == 9}. There are $tigerSpecies species of $animal") // The expression in curly braces evaluates to `true`
}
Qi avo kha tilulak $ glqbom es wiyg u dezu, neo sac irkubi ul cewf o nagyvpozk \:
fun main() {
val amount = 100_000_000
println("Gold is worth more than \$$amount")
}
Iv a sezfideso rcqarc, tattbawv iqwedexf am fax ewtabug. Muo oxpvueb saji jo kan tqu vhexojqim ri biynnm uvs mzu ptufujkoz ob cos is ek zimqno loebaw oh zexraoz mvu tixxw tsixuq uzn vjo $ duym. Voa zomd bahyolm owey ov dse haru zodim:
fun main() {
val amount = 25
val chat = """
Ann: How much does this book cost?
Bryan: This book costs $${amount}.
And you get a discount of ${'$'}9.99
"""
println(chat)
}
Ann: How much does this book cost?
Bryan: This book costs $25.
And you get a discount of $9.99
Ibion, yue doucn iba mafyef idjvewkaavx naxwam fme yildw thukez akg apoc nqmiay vgey ay gezmodke kizuh ryiz iyuvf u nejmoquku hsdimx:
fun main() {
val lowest = 45
val cash = 200
val books = 20
val chat = """
Ann: With $$cash how many books can I afford?
Bryan: ${ if (cash > lowest) 20 else 10 } books.
Ann: ${
if (books > 10) "Awesome!"
else "That's OK!"
}
"""
println(chat)
}
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.