如何在Swift中连接字符串? [英] How do I concatenate strings in Swift?
问题描述
如何在Swift中连接字符串?
How to concatenate string in Swift?
在 Objective-C
中,我们喜欢
NSString *string = @"Swift";
NSString *resultStr = [string stringByAppendingString:@" is a new Programming Language"];
或
NSString *resultStr=[NSString stringWithFormat:@"%@ is a new Programming Language",string];
但是我想用Swift语言做到这一点。
But I want to do this in Swift-language.
推荐答案
您可以通过多种方式连接字符串:
You can concatenate strings a number of ways:
let a = "Hello"
let b = "World"
let first = a + ", " + b
let second = "\(a), \(b)"
您也可以这样做:
var c = "Hello"
c += ", World"
我确定还有更多方法。
描述位
let
创建一个常量。 (有点像 NSString
)。设置后就无法更改其值。您仍然可以将其添加到其他内容中并创建新变量。
let
creates a constant. (sort of like an NSString
). You can't change its value once you have set it. You can still add it to other things and create new variables though.
var
创建一个变量。 (类似于 NSMutableString
),因此您可以更改其值。但这已经在Stack Overflow上得到了多次回答(请参阅 let和var之间的区别 ) 。
var
creates a variable. (sort of like NSMutableString
) so you can change the value of it. But this has been answered several times on Stack Overflow, (see difference between let and var).
注意
实际上是 let
和 var
与 NSString
和<$ c $有很大不同 c> NSMutableString ,但它有助于类比。
In reality let
and var
are very different from NSString
and NSMutableString
but it helps the analogy.
这篇关于如何在Swift中连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!