Append String in Swift

前端 未结 12 1279
伪装坚强ぢ
伪装坚强ぢ 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 01:04

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

提交回复
热议问题