Append String in Swift

前端 未结 12 1248
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 00:43

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 *str         


        
相关标签:
12条回答
  • 2020-12-03 00:48
    var string1 = "This is ";
    var string2 = "Swift Language";
    var appendString = string1 + string2;
    println("APPEND STRING: \(appendString)");
    
    0 讨论(0)
  • 2020-12-03 00:50

    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)")
    
    0 讨论(0)
  • 2020-12-03 00:53
    let firstname = "paresh"
    let lastname = "hirpara"
    let itsme = "\(firstname) \(lastname)"
    
    0 讨论(0)
  • 2020-12-03 00:55

    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)")
    
    0 讨论(0)
  • 2020-12-03 00:57
    let string2 = " there"
    var instruction = "look over"
    

    choice 1 :

     instruction += string2;
    
      println(instruction)
    

    choice 2:

     var Str = instruction + string2;
    
     println(Str)
    

    ref this

    0 讨论(0)
  • 2020-12-03 00:58

    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.

    0 讨论(0)
提交回复
热议问题