Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

Concatenate strings in Swift.

Home/ Questions/Q 221
Next
Answered
Concatenate strings in Swift.
aziz.sharopov2003
aziz.sharopov2003 Teacher

I’m a beginner in Swift and I’m trying to concatenate two strings together. I’ve tried using the + operator in between the two strings, but I’m getting an error saying that the operands don’t match. Can someone please help me figure out what I’m doing wrong?
Here’s the code that I’ve been trying to use:

let str1: String = "Hello"
let str2: String = "World"
let result: String = str1 + str2

When I try to run this code, I get the error message “Binary operator ‘+’ cannot be applied to two ‘String’ operands”. I’ve looked online to try to find a solution, but I haven’t been able to find any helpful information.
Can someone please explain to me what I’m doing wrong? How can I concatenate two string variables in Swift without getting this error? I appreciate any help that you can provide. Thank you!

ios-developmentstring-concatenationstring-manipulationswiftxcode
  • 723
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. marcofaifai Teacher
    2018-12-31T16:56:26+00:00Added an answer about 4 years ago

    One way to concatenate strings in Swift is by using the “+” operator. This operator can be used to combine two or more string values into one. Moreover, you can also use the “+=” operator to append a string value to an existing string variable.

    Another method for concatenation is by using the String format method. This function lets you create a string from a mix of constants, variables, literals, and expressions by including placeholders inside of it. By using this method, you can format different types of values such as integers, floats, and doubles along with strings in a specific format.

    Overall, the choice of method to use for concatenating strings in Swift depends on your specific requirements and the particular context of your application.

    • 36
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Best Answer
    auguscapomagi Begginer
    2018-12-17T01:32:43+00:00Added an answer about 4 years ago

    Hello there! I see you are having trouble with concatenating strings in Swift. Don’t worry, it’s a common issue that beginners face when they are learning the language. Concatenation simply means to combine two or more strings into one. There are a few ways you can concatenate strings in Swift.
    The easiest way to concatenate strings in Swift is by using the “+” operator. Simply add the “+” operator in between your two strings and they will be combined into one. Here’s an example:
    “`
    let firstName = “John”
    let lastName = “Doe”
    let fullName = firstName + ” ” + lastName
    “`
    The above code will result in a variable called “fullName” which will have the value “John Doe”. Just make sure you put a space between the two strings when you are adding the “+” operator.
    Another way to concatenate strings in Swift is by using string interpolation. This allows you to insert values into a string by wrapping them in a pair of parentheses and a backslash. Here’s an example:
    “`
    let temperature = 25
    let weather = “It’s (temperature) degrees outside”
    “`
    The above code will result in a variable called “weather” which will have the value “It’s 25 degrees outside”.
    You can also use the “+=” operator to concatenate strings. This operator will append a string to an existing one. Here’s an example:
    “`
    var sentence = “The quick brown fox”
    sentence += ” jumps over the lazy dog”
    “`

    The above code will result in a variable called “sentence” which will have the value “The quick brown fox jumps over the lazy dog”.

    In conclusion, there are multiple ways to concatenate strings in Swift. You can use the “+” operator, string interpolation, or the “+=” operator. Just pick the one that makes the most sense to you for any given situation. I hope this helps you with your coding journey!

    • 2
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.