如何在Swift连接字符串?

在Objective-C中

NSString *string = @"Swift";
NSString *resultStr = [string stringByAppendingString:@" is a new Programming Language"];

or

NSString *resultStr=[NSString stringWithFormat:@"%@ is a new Programming Language",string];

但我想用swift语言来写。


当前回答

它被称为字符串插值。 它是用常量,变量,字面量和表达式创建新字符串的方法。 例子:

      let price = 3
      let staringValue = "The price of \(price) mangoes is equal to \(price*price) "

let string1 = "anil"
let string2 = "gupta"
let fullName = string1 + string2  // fullName is equal to "anilgupta"
or 
let fullName = "\(string1)\(string2)" // fullName is equal to "anilgupta"

它也意味着串联字符串值。

希望这对你有所帮助。

其他回答

非常简单:

let StringA = "Hello"
let StringB = "World"
let ResultString = "\(StringA)\(StringB)"
println("Concatenated result = \(ResultString)")
var language = "Swift" 
var resultStr = "\(language) is a new programming language"

斯威夫特5:

数组的字符串变成一个单一的字符串

let array = ["Ramana","Meharshi","Awareness","Oneness","Enlightnment","Nothing"]
let joined = array.joined(separator: ",")

你现在可以在Swift中使用stringByAppendingString。

var string = "Swift"
var resultString = string.stringByAppendingString(" is new Programming Language")

你可以通过以下方式添加字符串:

str += “” str = str + “” str = str + str2 str = “” + “” str = “\(变量)” str = str + “\(变量)”

我想我把它们都叫出来了。