Swift中的String类型是否有trim方法?例如:
let result = " abc ".trim()
// result == "abc"
以下是如何从 String
的开头和结尾删除所有空格的方法。
(示例已在Swift 2.0中测试。)
let myString = " \t\t Let's trim all the whitespace \n \t \n "
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet()
)
// Returns "Let's trim all the whitespace"
(例子已经测试通过,适用于Swift 3+。)
let myString = " \t\t Let's trim all the whitespace \n \t \n "
let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines)
// Returns "Let's trim all the whitespace"
将此代码放入项目中的一个文件中,例如 Utils.swift:
extension String {
func trim() -> String {
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
}
那么你将能够做到这一点:
let result = " abc ".trim()
// result == "abc"
Swift 3.0 解决方案
extension String {
func trim() -> String {
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
}
}
那么您将能够做到这一点:
let result = " Hello World ".trim()
// result = "HelloWorld"
return self.trimmingCharacters(in: .whitespacesAndNewlines)
。 (说明:这是一行代码,用于在Swift 3编程语言中去除字符串开头和结尾的空格和换行符) - user5273262在Swift 3.0中
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
而且你可以打电话
let result = " Hello World ".trim() /* result = "Hello World" */
let trimmedString = " abc ".trimmingCharacters(in: .whitespaces)
//trimmedString == "abc"
let result = " abc ".trimmingCharacters(in: .whitespacesAndNewlines)
是的,可以这样做:
var str = " this is the answer "
str = str.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
print(srt) // "this is the answer"
CharacterSet实际上是一个非常强大的工具,用它可以创建比像.whitespacesAndNewlines这样的预定义集合更加灵活的修剪规则。
例如:
var str = " Hello World !"
let cs = CharacterSet.init(charactersIn: " !")
str = str.trimmingCharacters(in: cs)
print(str) // "Hello World"
CharacterSet.whitespaces
不再是一个函数!(同样的,NSCharacterSet.whitespaces
也不是)extension String {
func trim() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
extension String {
/// EZSE: Trims white space and new line characters
public mutating func trim() {
self = self.trimmed()
}
/// EZSE: Trims white space and new line characters, returns a new string
public func trimmed() -> String {
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
}
取自我的此存储库:https://github.com/goktugyil/EZSwiftExtensions/commit/609fce34a41f98733f97dfd7b4c23b5d16416206
将字符串截断到特定长度
如果你输入了一段句子/文字并且只想保存其中指定长度的文本。可以添加以下扩展到类中:
extension String {
func trunc(_ length: Int) -> String {
if self.characters.count > length {
return self.substring(to: self.characters.index(self.startIndex, offsetBy: length))
} else {
return self
}
}
func trim() -> String{
return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
使用
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
//str is length 74
print(str)
//O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
str = str.trunc(40)
print(str)
//O/P: Lorem Ipsum is simply dummy text of the
//移除 Swift 4.0 中的空格和换行符
extension String {
func trim() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}