Append String in Swift

IosObjective CSwift

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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)")

Solution 2 - Ios

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.

Solution 3 - Ios

let string2 = " there"
var instruction = "look over"

choice 1 :

 instruction += string2;
  
  println(instruction)

choice 2:

 var Str = instruction + string2;

 println(Str)

ref this

Solution 4 - Ios

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

Solution 5 - Ios

You can simply append string like:

var worldArg = "world is good"

worldArg += " to live";

Solution 6 - Ios

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!"

Solution 7 - Ios

var string1 = "This is ";
var string2 = "Swift Language";
var appendString = string1 + string2;
println("APPEND STRING: \(appendString)");

Solution 8 - Ios

> Swift2.x:

String("hello ").stringByAppendingString("world") // hello world

Solution 9 - Ios

Strings concatenate in Swift language.

let string1 = "one"

let string2 = "two"

var concate = " (string1) (string2)"

playgroud output is "one two"

Solution 10 - Ios

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)")

Solution 11 - Ios

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"

Solution 12 - Ios

let firstname = "paresh"
let lastname = "hirpara"
let itsme = "\(firstname) \(lastname)"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3733309View Question on Stackoverflow
Solution 1 - IosPREMKUMARView Answer on Stackoverflow
Solution 2 - IosIsaac DrachmanView Answer on Stackoverflow
Solution 3 - IosAnbu.KarthikView Answer on Stackoverflow
Solution 4 - IosEsqarrouthView Answer on Stackoverflow
Solution 5 - IosSaurav NagpalView Answer on Stackoverflow
Solution 6 - IosShailendra SuriyalView Answer on Stackoverflow
Solution 7 - IosnicaelView Answer on Stackoverflow
Solution 8 - IosMelvinView Answer on Stackoverflow
Solution 9 - IosShanmugasundharamView Answer on Stackoverflow
Solution 10 - IosROSSiDEASView Answer on Stackoverflow
Solution 11 - IosIlker BaltaciView Answer on Stackoverflow
Solution 12 - IosParesh HirparaView Answer on Stackoverflow