join()
数组中有什么用?
在其他语言中,它用于将数组的元素连接成字符串。例如,
Ruby Array.join
join()
数组中有什么用?
在其他语言中,它用于将数组的元素连接成字符串。例如,
Ruby Array.join
这是一个有用的字符串示例:
斯威夫特 3.0
let joiner = ":"
let elements = ["one", "two", "three"]
let joinedStrings = elements.joined(separator: joiner)
print("joinedStrings: \(joinedStrings)")
输出:
加入字符串:一:二:三
斯威夫特 2.0
var joiner = ":"
var elements = ["one", "two", "three"]
var joinedStrings = elements.joinWithSeparator(joiner)
print("joinedStrings: \(joinedStrings)")
输出:
加入字符串:一:二:三
斯威夫特 1.2:
var joiner = ":"
var elements = ["one", "two", "three"]
var joinedStrings = joiner.join(elements)
println("joinedStrings: \(joinedStrings)")
Obj-C 中的相同内容进行比较:
NSString *joiner = @":";
NSArray *elements = @[@"one", @"two", @"three"];
NSString *joinedStrings = [elements componentsJoinedByString:joiner];
NSLog(@"joinedStrings: %@", joinedStrings);
输出:
加入字符串:一:二:三