什么是去除斯威夫特字符串中的所有空间,\n
和\r
最有效的方法是什么?如何删除字符串中的所有空格和 n r?
我曾尝试:
for character in string.characters {
}
但它是一个有点不方便。
什么是去除斯威夫特字符串中的所有空间,\n
和\r
最有效的方法是什么?如何删除字符串中的所有空格和 n r?
我曾尝试:
for character in string.characters {
}
但它是一个有点不方便。
斯威夫特4:
let text = "This \n is a st\tri\rng"
let test = String(text.filter { !" \n\t\r".contains($0) })
输出:
print(test) // Thisisastring
虽然法赫里的回答是不错的,我喜欢它是纯粹的雨燕;)
使用此:
let aString: String = "This is my string"
let newString = aString.stringByReplacingOccurrencesOfString(" ", withString: "", options:[], range: nil)
print(newString)
输出: Thisismystring
您可以使用NSCharacterSet
whitespaceAndNewlineCharacterSet
分手您的字符串,并使用joinWithSeparator()
方法再次串连它如下:
let textInput = "Line 1 \n Line 2 \n\r"
let whitespaceAndNewlineCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
let components = textInput.componentsSeparatedByCharactersInSet(whitespaceAndNewlineCharacterSet)
let result = components.joinWithSeparator("") //"Line1Line2"
您还可以扩展字符串,使其更容易让你在你的代码的任何地方使用它:
extension String {
var removingWhitespacesAndNewlines: String {
return componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).joinWithSeparator("")
}
}
编辑/更新:
Swift3或更高版本
let textInput = "Line 1 \n Line 2 \n\r"
let result = textInput.components(separatedBy: .whitespacesAndNewlines).joined() //"Line1Line2"
extension String {
var removingWhitespacesAndNewlines: String {
return components(separatedBy: .whitespacesAndNewlines).joined()
}
}
let textInput = "Line 1 \n Line 2 \n\r"
let result = textInput.removingWhitespacesAndNewlines //"Line1Line2"
假设你有这样的字符串:“有些话这里\字nanother \ n \ r东西\ t和类似\ rmdjsbclsdcbsdilvb \ n \兰特最后这个:)”
这里如何删除所有可能的空间:
let possibleWhiteSpace:NSArray = [" ","\t", "\n\r", "\n","\r"] //here you add other types of white space
var string:NSString = "some words \nanother word\n\r here something \tand something like \rmdjsbclsdcbsdilvb \n\rand finally this :)"
print(string)// initial string with white space
possibleWhiteSpace.enumerateObjectsUsingBlock { (whiteSpace, idx, stop) -> Void in
string = string.stringByReplacingOccurrencesOfString(whiteSpace as! String, withString: "")
}
print(string)//resulting string
让我知道这对你的问题:)
斯威夫特4回应:
let text = "This \n is a st\tri\rng"
let cleanedText = text.filter { !" \n\t\r".characters.contains($0) }
为了完整起见,这是正则表达式版本
let string = "What is the most efficient way to remove all the spaces and \n \r \tin a String in Swift"
let stringWithoutWhitespace = string.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
// -> "WhatisthemostefficientwaytoremoveallthespacesandinaStringinSwift"
谢谢,这对我有用。 另外我想用一个空格替换所有连续的\ n,\ r,\ t或空格,所以我用“\\ s +”替换为“”。 let string =“this is a \ n \ t example” let newString = string.replacingOccurrences(of:“\\ s +”,with:“”,options:.regularExpression) // Result - >“this is一个例子“ – Chuy47
的SWIFT 4:
let myString = "This \n is a st\tri\rng"
let trimmedString = myString.components(separatedBy: .whitespacesAndNewlines).joined()
已经有一个完美的解决方案。 –
过滤器太昂贵,我认为对于初学者来说,这将更有用..这就是为什么我加了 – 2018-02-27 11:19:46
好吧,如果你认为那样就没问题。但请继续并请解释您的解决方案。 –
我想'filter'是这个太贵了,这有什么错'removeSubrange'或'stringByTrimmingCharacters'等? – kakubei
@kakubei:removeSubrange意味着你必须搜索整个字符串的子范围,在进行任何删除操作之前,已经像过滤一样昂贵。 stringByTrimmingCharacters只在字符串的开始和结尾修剪,而不在字的中间。 –