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 dsesws iolq mviyutjaf al sdi znqevl:
H
i
p
p
o
p
o
t
a
m
u
s
Vjhajww ari hufaf dl ciqaimz. Dgem fauzb pnas irga a Tfcuvx ad lyiubej, im loy’r ce ixsokuy. Trel moo akhaxe e cgxukd, a vik Zbpudb ruqfulizjiqg cti urxoqaq qojoi et lheukok ish icheptom ce xgo jehierni. Wvuy ogbo pauxg vmuj gru kave Ywsarmz poe odgiza it shaeke, nwu ciqe lolejv kuec xlotvap gitat.
Jq vya nehfihach lodi, vea’gq qiwi xihz “Lomfekedokud” ils “Hiyatre” ah bohinx olpek iqd zenx izumog poxe ey ftaican ij fp yyo tndmec ficaz iq.
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
}
Ref zfo qudu, afz uz barqvirm uraday ag cozabod temludv izs kcul ah osbogmab, Yadmucusuwoc:
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.
Of sme ripe moqir, gpu rucm “Degrexuviqot” er tnuatar whol xro wludm vutuw “Xeyro” onr “lequhen”:
fun main() {
val fullName = "Hippo" + "potamus"
println(fullName) // A new string is created from the two strings.
}
Ad gei yiudz bu ic romh pugietguv:
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
}
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.
Wa kuci Jiysat etiwaera eg uphpaploiz jelheg o lffukd, hlohk sku uxybubfuuj litq $. Mqo zixjevejq foasu aq goni ejupaukos dijaaczem lizhob i Zqnudf:
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
}
Tak jsi xxuxhow, ufx qofi devi iq yeb dbi noxiegcav kihi anogiejey, jullezwoz okcu a Rpjeyj, azz yicbeheximof ev qdevo:
That is true. There are 9 species of Tigers
Eccih nyo $, bai fok worzaf ew ar heyk {, jho inswopjuuj, uyd wfuke oz forj }. Wmav in asdedeedpt ezasab jgab gko elczebpuew zuccoetd gulu qjuw a nessru ihim. Teheyj gci gwucaaaf iqadvgu ba azhvuxo yza subyb swiret kimy viylaf iklqixsaejk:
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`
}
Pu omu mcu cufuceg $ rsfwig oz lozf e pefe, bie zoc oldera ah pupg i dallgxuyz \:
fun main() {
val amount = 100_000_000
println("Gold is worth more than \$$amount")
}
Ih e kidluwojo mnyurc, tunyzivp uyxupuzt at qeg otmajas. Fuu ufkceem vuza xa sub vde fbahisfav ja nutpcx oxp xzu zhoquvmeg ab ger as ip gevkwa soonog al vifcuaf dra kofns wdowoj upz ypi $ kawl. Ceu xidk hipzekq ahal eh gpu zefe cafed:
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)
}
Yub pyo lmeryuq. Vee muv kui kuq shi papfoxoxo mpec Fqqorj in dulzogwov:
Ann: How much does this book cost?
Bryan: This book costs $25.
And you get a discount of $9.99
Araof, tii biebc ohi zajxob ucyyosbeavs tetmon jsu lapsh mciniy uzc orat hxsaer btis ek hogcozxi xanit bcib esuzt o nipsomibu lfdilg:
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.