Append String in Swift
Asked Answered
N

12

76

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.

Namnama answered 12/6, 2014 at 9:1 Comment(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 developer.apple.com/library/prerelease/ios/documentation/swift/…Garlandgarlanda
G
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)")
Gev answered 12/6, 2014 at 9:4 Comment(3)
In ObjC there's also appendString2 = [string1 stringByAppendingString: string2];Lynnelynnea
Thanks, mate. All this Obj-C to Swift change is messing things up.Corporative
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).Helprin
A
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.

Adjudicate answered 12/6, 2014 at 9:4 Comment(0)
O
8
let string2 = " there"
var instruction = "look over"

choice 1 :

 instruction += string2;

  println(instruction)

choice 2:

 var Str = instruction + string2;

 println(Str)

ref this

Omaomaha answered 12/6, 2014 at 9:6 Comment(0)
B
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

Berne answered 24/7, 2015 at 13:37 Comment(1)
Shouldn't the 3rd line be var str1.addString(str2)Spectrogram
I
5

You can simply append string like:

var worldArg = "world is good"

worldArg += " to live";
Ideograph answered 12/6, 2014 at 9:24 Comment(0)
H
4
var string1 = "This is ";
var string2 = "Swift Language";
var appendString = string1 + string2;
println("APPEND STRING: \(appendString)");
Hinny answered 12/6, 2014 at 9:3 Comment(0)
E
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!"
Elamitic answered 18/1, 2018 at 22:51 Comment(0)
M
1

> Swift2.x:

String("hello ").stringByAppendingString("world") // hello world
Mnemonic answered 12/8, 2016 at 9:58 Comment(0)
R
0

Strings concatenate in Swift language.

let string1 = "one"

let string2 = "two"

var concate = " (string1) (string2)"

playgroud output is "one two"

Rigidify answered 16/4, 2015 at 5:31 Comment(0)
H
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)")
Halley answered 9/1, 2016 at 15:22 Comment(0)
R
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"
Rooky answered 12/1, 2017 at 14:9 Comment(0)
G
0
let firstname = "paresh"
let lastname = "hirpara"
let itsme = "\(firstname) \(lastname)"
Globate answered 5/9, 2017 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.