如何在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 StringA = "Hello"
let StringB = "World"
let ResultString = "\(StringA)\(StringB)"
println("Concatenated result = \(ResultString)")

其他回答

这也会起作用:

var string = "swift"
var resultStr = string + " is a new Programming Language"

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

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

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

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

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

斯威夫特5

你可以使用附加API来实现它。这将返回一个通过将给定字符串追加到接收器而生成的新字符串。

API详情:此处

Use:

var text = "Hello"
text = text.appending(" Namaste")

结果:

Hello
Hello Namaste

斯威夫特5:

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

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