What exactly String interpolation in swift means? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What exactly String interpolation in swift means?

I don't understand the formal definition given by SoloLearn , can someone explain me with a example for my low iq brain XD

7th Sep 2020, 3:40 PM
Sreenesh
Sreenesh - avatar
3 Answers
+ 4
Basically it's a replacement of a placeholder within a string by the string representation of a variable's content. A placeholder is a part of the string marked by a backslash and followed by an identifier's name wrapped in parentheses. * Example: var season = "Summer" var year = 2020 print("It's \(season) season of \(year)") WHERE: \(season) and \(year) are placeholders season, year are identifiers The part of the string occupied by placeholders will be replaced by content of respective identifier <season> and <year> . Output: It's Summer season of 2020 Just what I learned from a question in forum (cmiiw) ... String interpolation also works for constants, literals and expressions (Thanks for the note Arsenic). (Edited)
7th Sep 2020, 4:05 PM
Ipang
+ 3
It is a way to creating a string from a mix of constants, variables, literals, and expressions. Taking Ipang 's example, When you typed (" it's \(seasons) season of \(year)") Then entire thing inside the double quotes (" ") is converted to string after filling it with the correct values of placeholders *seasons* and *year*.
7th Sep 2020, 4:19 PM
Arsenic
Arsenic - avatar
+ 2
Thanks for explaination!
7th Sep 2020, 4:17 PM
Sreenesh
Sreenesh - avatar