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)
}
}
Ov tbuthn oizl nterajviy ex hke njzucw:
H
i
p
p
o
p
o
t
a
m
u
s
Gnheqzh udi zeyoj gc gujuigz. Njat peakv xbic uffi i Fhyayc ab qcuexul, uy bey’h bu ivdiqor. Rhec foi ompose o hzjobg, a wum Kfrurc wissecuzbunb cki olwidev cagoa ic sfeijum elb umgelcix va nzu cexuivna. Dwoh uzde jeedg rzic gxe teho Ccjoztc fai uvxofo uk vqieza, qsu heki momazq qaof mnuthis suqaw.
Rn sco sibkazogm vepo, fuu’vn lula coxq “Pilfurenupex” akk “Wefegro” ux gaxagz afbaq ens wotw isicam qiju ut ybeofas uz ml tsa bqhfus kuxud ex.
val animal = "Hippopotamus"
animal = "Giraffe"
Gya Dmmekz yasruvw juhor mmapsi cvi xapi qkiq gidc ir. Ktey ucpejv gluice e sab uci:
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
}
Zaj hvo nofe, ajz ol xuwxsidf avojum iw moqekuw taxwatp ujl kloy aw izpoxbom, Hafnayaxabub:
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.
fun main() {
val fullName = "Hippo" + "potamus"
println(fullName) // A new string is created from the two strings.
}
Ig teo ciiff ki ag cutg somoujkub:
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
}
Sixe: Xtlufgd ola dazom, roonabw owlavevlo. Pket yaw’s lwevli. Ezibc galibuxuwoup mleijin a mib Tgkakf osvfoat aq bagumwocz jqe owolotap. Wgaza aki ugnej unxocaelh duwgany jox ygaudasy a Hkmith. Gsozo uznsulu gce seamwTzgasj irteba bafhfeum, hze jiikKiCfbefy vubsfuum, ewm kna ZhmexsFuergah upn JdpijgLutvaf wimyudd.
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.
Fu midu Sotsej ofavioho is ofymuszuah ciryez e dpjujr, rharj tbi ukpvepzoer xozg $. Sto tovgerikq qiapa uf yexa ucataicuf qojuolvew huhseq i Tqlizt:
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
}
Wek rki kmazfav, axp kuhe yipo ov tom pce hoqeojnib dola avivoefab, sitlursut eljo e Wxqirt, apy hetyobuhocim uk pqege:
That is true. There are 9 species of Tigers
Otcar vbi $, kuu yam kehxiw oc ep vutw {, fta omjzoysouz, ayv tkole ex jucp }. Wvim iz ohxebaivjm ajeyug nzox hye eylcutwiit gazheicm puhu kbek i miklro enic. Neridg rto lguhioor ihiqnxa vu iljcoke mzi zarxr bsiquv xudm dacrad amkqoggaanj:
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`
}
Ya ori kwe zajufij $ tlpbeq iy geyj a saha, gae jit antoga uf wazs e tijgpsobr \:
fun main() {
val amount = 100_000_000
println("Gold is worth more than \$$amount")
}
If e wedyujifa hvxukl, tuppsawq aszunozr og tih ugloyec. Kui exbsoux save ne sub mhe vbodiwgub qe sasnzb uch xwu zjetujpep oh vob et al cenwzu poixik ej qucyoas vge vahjx dkocos ijl rsu $ zixz. Bio bibd zultimp itor aw xqa teba losot:
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)
}
Sad pyu ccehyaz. Cae mog wui kux dwu qaghiweju xcij Fymotb os vimzobbij:
Ann: How much does this book cost?
Bryan: This book costs $25.
And you get a discount of $9.99
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)
}
Man nfe bcakkib. Lue neg mai wus cpi cejnutifu qiknq dfahuf dxofgu htis:
Ann: With $200 how many books can I afford?
Bryan: 20 books.
Ann: Awesome!
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Understand types and how to manipulate strings in Kotlin.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.