我知道如何通过编程来做到这一点,但我相信有一种内置的方式……
我使用过的每种语言都有某种对象集合的默认文本表示,当您试图将Array与字符串连接起来或将其传递给print()函数等时,它会吐出这些文本表示。苹果的Swift语言是否有一种内置的方式,可以轻松地将数组转换为字符串,或者我们总是必须显式地对数组进行字符串化?
我知道如何通过编程来做到这一点,但我相信有一种内置的方式……
我使用过的每种语言都有某种对象集合的默认文本表示,当您试图将Array与字符串连接起来或将其传递给print()函数等时,它会吐出这些文本表示。苹果的Swift语言是否有一种内置的方式,可以轻松地将数组转换为字符串,或者我们总是必须显式地对数组进行字符串化?
当前回答
对于swift 3:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == phoneField
{
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
let components = newString.components(separatedBy: NSCharacterSet.decimalDigits.inverted)
let decimalString = NSString(string: components.joined(separator: ""))
let length = decimalString.length
let hasLeadingOne = length > 0 && decimalString.character(at: 0) == (1 as unichar)
if length == 0 || (length > 10 && !hasLeadingOne) || length > 11
{
let newLength = NSString(string: textField.text!).length + (string as NSString).length - range.length as Int
return (newLength > 10) ? false : true
}
var index = 0 as Int
let formattedString = NSMutableString()
if hasLeadingOne
{
formattedString.append("1 ")
index += 1
}
if (length - index) > 3
{
let areaCode = decimalString.substring(with: NSMakeRange(index, 3))
formattedString.appendFormat("(%@)", areaCode)
index += 3
}
if length - index > 3
{
let prefix = decimalString.substring(with: NSMakeRange(index, 3))
formattedString.appendFormat("%@-", prefix)
index += 3
}
let remainder = decimalString.substring(from: index)
formattedString.append(remainder)
textField.text = formattedString as String
return false
}
else
{
return true
}
}
其他回答
斯威夫特3
["I Love","Swift"].joined(separator:" ") // previously joinWithSeparator(" ")
在Swift 4中
let array:[String] = ["Apple", "Pear ","Orange"]
array.joined(separator: " ")
修改一个可选/非可选字符串数组
//Array of optional Strings
let array : [String?] = ["1",nil,"2","3","4"]
//Separator String
let separator = ","
//flatMap skips the nil values and then joined combines the non nil elements with the separator
let joinedString = array.flatMap{ $0 }.joined(separator: separator)
//Use Compact map in case of **Swift 4**
let joinedString = array.compactMap{ $0 }.joined(separator: separator
print(joinedString)
这里是flatMap, compactMap跳过数组中的nil值,并附加其他值以给出一个连接字符串。
如果数组包含字符串,你可以使用String的join方法:
var array = ["1", "2", "3"]
let stringRepresentation = "-".join(array) // "1-2-3"
在Swift 2中:
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
如果您想使用特定的分隔符(连字符、空格、逗号等),这可能很有用。
否则,你可以简单地使用description属性,它返回数组的字符串表示形式:
let stringRepresentation = [1, 2, 3].description // "[1, 2, 3]"
提示:任何实现Printable协议的对象都有一个description属性。如果你在自己的类/结构中采用该协议,你也可以使它们打印友好
在Swift 3中
join变为joined,示例[nil, "1", "2"].flatMap({$0}).joined() joinWithSeparator变成了joined(separator:)(仅适用于字符串数组)
在Swift 4中
var array = ["1", "2", "3"]
array.joined(separator:"-")
你可以使用循环来完成这个任务。或者使用地图。
通过映射:
let array = ["one" , "two" , "three"]
array.map({$0}).joined(seperator : ",")
在分隔符中,你可以修改字符串。
Output-> ("one,two,three")