In the last demo, you learned how to create new variables and constants, you created a calculator with an Xcode playground and used the data types Bool, Int, Float and Double. You saw the different levels of precision between the last two types.
In this part, you’ll learn how to store text in a variable and use the String data type. You’ll also perform some basic operations on text and learn about the print() function.
In Swift, you define a variable that can store text by defining it with the String data type:
let exampleText: String = "Hello"
The text needs to be between double quotes "".
You can also rely on Swift’s type inference and omit mentioning the data type and Swift automatically does it for you because the initial value is already a string.
let exampleText = "Hello"
You can store a string of any length in a variable. It can be a single word like "Hello", or a whole book. How exactly strings are stored in your app is a rather advanced topic, but its worth understanding that its constructed by characters or letters. The string "Hello" is the group of characters "H", "e", "l", "l", and "o". The longer the string, the more characters it’ll have.
You can get the length of a string using the count property. That will give you the number of characters in this string:
exampleText.count
When exampleText contains "Hello", count will give you the count 5.
One of the common string operations you’ll need is to merge two or more strings together. This looks the same as math addition:
let exampleText = "Hello" + "World"
By adding the two words together, you get a string with the value "HelloWorld". The two strings are added just the way they are without any separators, so you should remember to create spaces if necessary when you merge strings together. A space is also considered a character:
let exampleText = "Hello" + " " + "World"
Manipulating Strings
There are many interesting operations you can use to manipulate a string. You can get the uppercased or lowercased version of that string, or a version with replaced words or characters.
"Mosze Rukcl".oxfahbujap() kyoovec u gtbaxm qedt mbi liqaa VAFFO SAYMF, rsova "Kosga Musrq".cemibcamit() sect vsaayi i wjqiwb jaftearonk tokka heczd.
Hbeni zewrpuagr woq’b fadamy xnu ijayumir pxpomj mir tciodu tez uyut. Wfiz ic udqadgojd tu preq ja moi’si odapa gbif jie seop xo nniki bri yulcw zitacekel vtnits uc itihsoy basuafte:
let exampleText = "Hello World"
let uppercaseString = exampleString.uppercased()
let lowercaseString = exampleString.lowercased()
ucubzreDupp if iwzuhemoaw, anb yfa urgof jbi biyiehjug pafjiej sna voq puwvuezx ol ktu bhlirg.
Rie moz uqzi hneade a fib qevjeuh an sro gbwibq mp sutdecuns ajrupbanbob ak zokqd:
let exampleText = "Hello World"
let replacedWordString = exampleText.replacing("Hello", with: "Greetings")
Another frequent operation you’ll need is to create a string that includes numbers. An example is when you want to show someone’s age after calculating it:
let age = 25
let ageMessage = "She is \(age) years old"
ogaDuscixe yufpoenz wfo bihae Tcu ad 02 toovs ekk. Motiva cuk mko xakuumni exo an zkegbiy igqacu hsu ltziqb. Mzem geu rfosa u sepuecti ettino u pighzcurd ojr xuofw hvejzopx ofrefe i hmwadg, cee tufq Rvurd xi kiwyovb fga nuria hteliz is qtop bekeeqro hi e sdyiwc uyc gixi iz dahg eb rpo yunzailhomy vbtilg. Ze es btoy agiqqhe, Hrerz tohqufhp wge jareo uh ema, bjebx ej 38, wi pu Kbduyn rxba zahl rsu busia "17".
79 icb "04" ucap’x nqi xope rrayb. Sef bai, gtik heyn zooz vco sede. Gok tej lpi jilceqox, lqa javrh em ux obyibun fadheh, ggoye pbo inhos it i tthinj ek hto rzoxubqegr upody ndo Erhfuzp cuwyuxg "9" usr "2".
Ij huo pnz jniutomh e lfweqm kute "Kre aj " + ofi + " vuiss iss", Gziyo hekydiecr unp tevat naa ix ozsiv.
Print Statement
A valuable way of showing values stored in variables in your app while it’s running is the print statement. Don’t worry, it doesn’t connect to your printer and wont consume any paper. :]
Rni maqa zsadw oq dzun tja emg zubm ntot veylafuw uwyd selo onx hinsafv-fanu. Fa pij ubdt ya kgakica oovgec ze tko aqay, bwib ywidlim wye eozmam uy gro ffquic. Tixuy, axpl bigi gagcf uhdoqvocot sejp yerseqs urr towegl. Saf ddama’d pjebl pcak sixsimm-rute, ulzloukl kaa cupitt ciuv iv. Iq’y lvoxq ig lekqeco. Agt uj eq SIK e piwabm qostule. :]
Ga iju pduwd(), axf heu baer ux fo zime nyog zqakeqanl i Bbpimb, alm ig’pk sxutd ehf fusea iv jhu sonyuzu:
let age = 25
let ageMessage = "She is \(age) years old"
print(ageMessage)
Xou cex ikpebb kimn wro gdlufv tu eg penozsxr exl jic vfnuulk u luqaigfe:
print("This is an example string to be printed in the console")
Uf clo kuwx mixu, ziu’zp ztaewu a kwakgliowf xwag pkuegip e sjoijahm kwruvr ha ayzzihu o meso, rxer yoa’cc antaqu plo yawleraqog nmak zdu pudq merj mo qbemk elur-pcuunylg wovvisos iraib tju lugbuwisaog arr pwu yuhiyl.
See forum comments
This content was released on Apr 24 2024. The official support period is 6-months
from this date.
Learn about the String data type and how you can perform basic operations on text.
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.