4

I was wondering the best way to add to a String in Swift 4. Did Apple create a better way than stringName.characters.append(“hi”)

Edit: Noted below which I never knew you used to be able to use a String.append(). I was trying to get at the fact that in Swift 4 you don't have to use .characters anymore. I was trying to help out new people to swift 4 by making a question that they might ask so that they can save time by not using .characters after a String variable.

1
  • 4
    It was always possible to use string.append(anotherString) Commented Jul 30, 2017 at 16:35

2 Answers 2

13

You can invoke append(_:) directly on the String instance:

var stringName = ""
stringName.append("hi")
print(stringName) // hi
stringName.append(" John")
print(stringName) // hi John

Likewise, you can use the += operator of String for the concatenation

var stringName = ""
stringName += "hi"
print(stringName) // hi
stringName += " John"
print(stringName) // hi John

For the curious one, the implementation of both of these approaches make use of the same backend (/core) append(...) call. Quoting swift/stdlib/public/core/String.swift:

extension String {

  // ...

  public mutating func append(_ other: String) {
    _core.append(other._core)
  }

  // ...
}

extension String {

  // ...

  public static func += (lhs: inout String, rhs: String) {
    if lhs.isEmpty {
      lhs = rhs
    }
    else {
      lhs._core.append(rhs._core)
    }
  }

  // ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome thanks. I do know the answer(I was at WWDC), but trying to get my rep up with questions and stuff. And figured someone will be asking.
@the_kaseys if you have a question that you already know the answer to, and believe this could be a valuable Q&A for other visitors here at SO (possibly not trivial questions that are already well covered by the language reference), you may ask and answer your own question. Answering other's questions is very much more common though, and can also be good practice for the answerer (I've learnt a lot in the process of writing my SO answers).
Oh dang my bad. I appreciate you letting me know! I just got started with SO.
@the_kaseys no worries, and welcome to SO! Make sure to take your time reading through the Asking section in the SO Help Center (probably most importantly the subsections coverings MVCE and How do I ask a good question subsections), which contains lots of valuable information for SO newcomers, especially if you want to post questions of your own!
0

According to Swift 4 Documentation, String values can be added together (or concatenated) with the addition operator (+) to create a new String value:

let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"

You can also append a String value to an existing String variable with the addition assignment operator (+=):

var instruction = "look over"
instruction += string2
// instruction now equals "look over there"

You can append a Character value to a String variable with the String type’s append() method:

let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.