ios 在 Swift 中追加字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24180346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append String in Swift
提问by PREMKUMAR
I am new to iOS. I am currently studying iOS using Objective-C and Swift.
我是 iOS 新手。我目前正在使用 Objective-C 和 Swift 学习 iOS。
To append a string in Objective-C I am using following code:
要在 Objective-C 中附加一个字符串,我使用以下代码:
NSString *string1 = @"This is";
NSString *string2 = @"Swift Language";
NSString *appendString=[NSString stringWithFormat:@"%@ %@",string1,string2];
NSLog(@"APPEND STRING:%@",appendString);
Anyone please guide me.
任何人请指导我。
回答by PREMKUMAR
Its very simple:
它非常简单:
For ObjC:
对于 ObjC:
NSString *string1 = @"This is";
NSString *string2 = @"Swift Language";
ForSwift:
ForSwift:
let string1 = "This is"
let string2 = "Swift Language"
For ObjC AppendString:
对于 ObjC AppendString:
NSString *appendString=[NSString stringWithFormat:@"%@ %@",string1,string2];
For Swift AppendString:
对于 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:
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)")
回答by Isaac Drachman
In Swift, appending strings is as easy as:
在 Swift 中,附加字符串非常简单:
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.
注意:我看到其他答案正在使用var
很多。字符串没有改变,因此应该使用let
. 我知道这是一个小练习,但养成最佳实践的习惯是件好事。尤其是因为这是 Swift 的一大特色。
回答by Anbu.Karthik
回答by Esqarrouth
Add this extension somewhere:
在某处添加此扩展名:
extension String {
mutating func addString(str: String) {
self = self + str
}
}
Then you can call it like:
然后你可以这样称呼它:
var str1 = "hi"
var str2 = " my name is"
str1.addString(str2)
println(str1) //hi my name is
A lot of good Swift extensions like this are in my repo here, check them out: https://github.com/goktugyil/EZSwiftExtensions
很多像这样的优秀 Swift 扩展都在我的仓库中,请查看:https: //github.com/goktugyil/EZSwiftExtensions
回答by Saurav Nagpal
You can simply append string like:
您可以简单地附加字符串,如:
var worldArg = "world is good"
worldArg += " to live";
回答by nicael
var string1 = "This is ";
var string2 = "Swift Language";
var appendString = string1 + string2;
println("APPEND STRING: \(appendString)");
回答by Shailendra Suriyal
According to Swift 4 Documentation, String values can be added together (or concatenated) with the addition operator (+) to create a new String value:
根据Swift 4 Documentation,可以使用加法运算符 (+) 将字符串值加在一起(或连接)以创建新的字符串值:
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:
您可以使用 String 类型的 append() 方法将 Character 值附加到 String 变量:
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"
回答by Melvin
> Swift2.x:
> Swift2.x:
String("hello ").stringByAppendingString("world") // hello world
回答by Ilker Baltaci
SWIFT 2.x
斯威夫特 2.x
let extendedURLString = urlString.stringByAppendingString("&requireslogin=true")
SWIFT 3.0
斯威夫特 3.0
From Documentation: "You can append a Character value to a String variable with the String type's append() method:"so we cannot use append for Strings.
来自文档: “您可以使用 String 类型的 append() 方法将 Character 值附加到 String 变量:”因此我们不能对字符串使用 append。
urlString += "&requireslogin=true"
"+" Operator works in both versions
“+”运算符适用于两个版本
let extendedURLString = urlString+"&requireslogin=true"
回答by Paresh Hirpara
let firstname = "paresh"
let lastname = "hirpara"
let itsme = "\(firstname) \(lastname)"