samatron
samatron

Reputation: 477

How to append a character to a string in Swift?

This used to work in Xcode 6: Beta 5. Now I'm getting a compilation error in Beta 6.

for aCharacter: Character in aString {
    var str: String = ""
    var newStr: String = str.append(aCharacter) // ERROR
    ...
}

Error: Cannot invoke append with an argument of type Character

Upvotes: 38

Views: 53520

Answers (9)

Jiraheta
Jiraheta

Reputation: 481

for those looking for swift 5, you can do interpolation.

var content = "some random string"
content = "\(content)!!"
print(content) // Output: some random string!!

Upvotes: 0

Gr8Warrior
Gr8Warrior

Reputation: 719

According to Swift 4 Documentation , You can append a Character value to a String variable with the String type’s append() method:

var welcome = "hello there"

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

Upvotes: 2

ICL1901
ICL1901

Reputation: 7778

I had to get initials from first and last names, and join them together. Using bits and pieces of the above answers, this worked for me:

  var initial: String = ""

            if !givenName.isEmpty {
                let char = (givenName as NSString).substring(with: NSMakeRange(0, 1))
               let str = String(char)
                initial.append(str)
            }

            if !familyName.isEmpty {
                 let char = (familyName as NSString).substring(with: NSMakeRange(0, 1))
                let str = String(char)
                initial.append(str)
            }

Upvotes: 0

Yogesh shelke
Yogesh shelke

Reputation: 468

 let original:String = "Hello"
 var firstCha = original[original.startIndex...original.startIndex]

 var str = "123456789"
 let x = (str as NSString).substringWithRange(NSMakeRange(0, 4))

 var appendString1 = "\(firstCha)\(x)" as String!
 // final name
 var namestr = "yogesh"
 var appendString2 = "\(namestr) (\(appendString1))" as String!*

Upvotes: -1

Gary Makin
Gary Makin

Reputation: 3169

Update for the moving target that is Swift:

Swift no longer has a + operator that can take a String and an array of characters. (There is a string method appendContentsOf() that can be used for this purpose).

The best way of doing this now is Martin R’s answer in a comment below:

var newStr:String = str + String(aCharacter)

Original answer: This changed in Beta 6. Check the release notes.I'm still downloading it, but try using:

var newStr:String = str + [aCharacter]

Upvotes: 46

Mike Vosseller
Mike Vosseller

Reputation: 4197

append append(c: Character) IS the right method but your code has two other problems.

The first is that to iterate over the characters of a string you must access the String.characters property.

The second is that the append method doesn't return anything so you should remove the newStr.

The code then looks like this:

for aCharacter : Character in aString.characters {
    var str:String = ""
    str.append(aCharacter)
    // ... do other stuff
}

Upvotes: 9

user3182143
user3182143

Reputation: 9589

var stringName: String = "samontro"
var characterNameLast: Character = "n"
stringName += String(characterNameLast) // You get your name "samontron"

Upvotes: 1

hennes
hennes

Reputation: 9342

Another possible option is

var s: String = ""
var c: Character = "c"
s += "\(c)"

Upvotes: 6

baskInEminence
baskInEminence

Reputation: 762

This also works

var newStr:String = str + String(aCharacter)

Upvotes: 11

Related Questions