Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Egor Bedunkevich
Egor Bedunkevich
643 Points

String Interpolation and Concatenation...

I just had a question that I was wondering about. I read an article about concatenation and they called it "string concatenation". I also know about String Interpolation. Is there such thing as Integer Concatenation or Interpolation? Furthermore, Does concatenation only work with strings or the same value? Can I please get a detailed response. (What is the difference between String Concatenation and concatenation) Is it the same thing?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Egor,

In JavaScript it's possible to join two numbers together which Charles Guilbert mentioned, however in a language such as Swift this isn't available which is where string interpolation comes into effect as that can be used to add numbers into strings as the compiler will automatically convert the base value into the type String.

For example:

// This will fail and cause a compiler error
var myNumber = 10
var myString = "My pet chicken laid " + myNumber + " eggs"

// This will work as we're using string interpolation
var myNumber = 10
var myString = "My pet chicken laid \(myNumber) eggs"

Hope that helps.

Concatenation is the joining together of two values in a string format. For example '2'+'1' = 21 in a concatenation as it is treating the 2 and the 1 as strings and not integers. Without treating the 2 and 1 as strings 2+1 would equal 3. So to answer your question, yes they are the same thing.