75

I am new to iOS. I am currently studying iOS using Objective-C and Swift.

To append a string in Objective-C I am using following code:

 NSString *string1 = @"This is";
 NSString *string2 = @"Swift Language";
 NSString *appendString=[NSString stringWithFormat:@"%@ %@",string1,string2];
 NSLog(@"APPEND STRING:%@",appendString);

Anyone please guide me.

jluckyiv
  • 3,691
  • 3
  • 22
  • 15
  • 1
    If you're completely new to both Objective C and Swift in iOS, I'd possibly choose one to get familiar with before expanding into the other. Don't want to be taking on too much too quickly. Everything about strings in Swift, including appending, can be found at https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/StringsAndCharacters.html – biddulph.r Jun 12 '14 at 09:04

12 Answers12

117

Its very simple:

For ObjC:

     NSString *string1 = @"This is";
     NSString *string2 = @"Swift Language";

ForSwift:

    let string1 = "This is"
    let string2 = "Swift Language"

For ObjC AppendString:

     NSString *appendString=[NSString stringWithFormat:@"%@ %@",string1,string2];

For Swift AppendString:

    var appendString1 = "\(string1) \(string2)"
    var appendString2 = string1+string2

Result:

    print("APPEND STRING 1:\(appendString1)")
    print("APPEND STRING 2:\(appendString2)")

Complete Code In Swift:

    let string1 = "This is"
    let string2 = "Swift Language"
    var appendString = "\(string1) \(string2)"
    var appendString1 = string1+string2
    print("APPEND STRING1:\(appendString1)")
    print("APPEND STRING2:\(appendString2)")
PREMKUMAR
  • 8,283
  • 8
  • 28
  • 51
  • 1
    In ObjC there's also `appendString2 = [string1 stringByAppendingString: string2];` – JeremyP Jul 24 '15 at 13:53
  • Thanks, mate. All this Obj-C to Swift change is messing things up. – Felipe Nov 26 '15 at 12:42
  • Worth noting that, with the more strings you're appending at once, using the style in `appendString1` gets a lot more performant than concatenating (`appendString2`). – Connor Neville Jan 12 '17 at 14:13
20

In Swift, appending strings is as easy as:

let stringA = "this is a string"
let stringB = "this is also a string"
let stringC = stringA + stringB

Or you can use string interpolation.

let stringC = "\(stringA) \(stringB)"

Notice there will now be whitespace between them.

Note: I see the other answers are using var a lot. The strings aren't changing and therefore should be declared using let. I know this is a small exercise, but it's good to get into the habit of best practices. Especially because that's a big feature of Swift.

Isaac Drachman
  • 984
  • 6
  • 8
8
let string2 = " there"
var instruction = "look over"

choice 1 :

 instruction += string2;

  println(instruction)

choice 2:

 var Str = instruction + string2;

 println(Str)

ref this

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
8

Add this extension somewhere:

extension String {
    mutating func addString(str: String) {
        self = self + str
    }
}

Then you can call it like:

var str1 = "hi"
var str2 = " my name is"
str1.addString(str2)
println(str1) //hi my name is

A lot of good Swift extensions like this are in my repo here, check them out: https://github.com/goktugyil/EZSwiftExtensions

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
5

You can simply append string like:

var worldArg = "world is good"

worldArg += " to live";
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Saurav Nagpal
  • 1,247
  • 1
  • 10
  • 27
4
var string1 = "This is ";
var string2 = "Swift Language";
var appendString = string1 + string2;
println("APPEND STRING: \(appendString)");
nicael
  • 18,550
  • 13
  • 57
  • 90
4

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!"
Gr8Warrior
  • 699
  • 7
  • 11
1

> Swift2.x:

String("hello ").stringByAppendingString("world") // hello world
Melvin
  • 3,421
  • 2
  • 37
  • 41
0

Strings concatenate in Swift language.

let string1 = "one"

let string2 = "two"

var concate = " (string1) (string2)"

playgroud output is "one two"

Shanmugasundharam
  • 2,082
  • 23
  • 32
0

In the accepted answer PREMKUMAR there are a couple of errors in his Complete code in Swift answer. First print should read (appendString) and Second print should read (appendString1). Also, updated println deprecated in Swift 2.0

His

let string1 = "This is"
let string2 = "Swift Language"
var appendString = "\(string1) \(string2)"
var appendString1 = string1+string2
println("APPEND STRING1:\(appendString1)")
println("APPEND STRING2:\(appendString2)")

Corrected

let string1 = "This is"
let string2 = "Swift Language"
var appendString = "\(string1) \(string2)"
var appendString1 = string1+string2
print("APPEND STRING:\(appendString)")
print("APPEND STRING1:\(appendString1)")
ROSSiDEAS
  • 21
  • 4
0

SWIFT 2.x

let extendedURLString = urlString.stringByAppendingString("&requireslogin=true")

SWIFT 3.0

From Documentation: "You can append a Character value to a String variable with the String type’s append() method:" so we cannot use append for Strings.

urlString += "&requireslogin=true"

"+" Operator works in both versions

let extendedURLString = urlString+"&requireslogin=true"
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
0
let firstname = "paresh"
let lastname = "hirpara"
let itsme = "\(firstname) \(lastname)"
Paresh Hirpara
  • 487
  • 3
  • 10