Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,650 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.