What is the most efficient way to remove all the spaces, \n
and \r
in a String in Swift?
删除Swift中String中所有空格,\ n和\ r的最有效方法是什么?
I have tried:
我努力了:
for character in string.characters {
}
But it's a little inconvenient.
但这有点不方便。
7 个解决方案
#1
37
Swift 4:
斯威夫特4:
let text = "This \n is a st\tri\rng"
let test = String(text.filter { !" \n\t\r".contains($0) })
Output:
输出:
print(test) // Thisisastring
While Fahri's answer is nice, I prefer it to be pure Swift ;)
虽然Fahri的答案很好,但我更喜欢纯粹的Swift;)
#2
6
You can use NSCharacterSet
whitespaceAndNewlineCharacterSet
to break up your string and use joinWithSeparator()
method to concatenate it back again as follow:
您可以使用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"
You can also extend String to make it easier for you to use it anywhere in your code:
您还可以扩展String以使您更容易在代码中的任何位置使用它:
extension String {
var removingWhitespacesAndNewlines: String {
return componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).joinWithSeparator("")
}
}
edit/update:
编辑/更新:
Swift3 or later
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"
#3
3
For the sake of completeness this is the Regular Expression version
为了完整起见,这是正则表达式版本
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"
#4
1
Suppose that you have this string : "some words \nanother word\n\r here something \tand something like \rmdjsbclsdcbsdilvb \n\rand finally this :)"
假设你有这个字符串:“有些单词\ nanother单词\ n \ r这里有东西\ t和类似\ rmdjsbclsdcbsdilvb \ n \ rand这样的东西:)”
here the how to remove all possible space :
这里有如何删除所有可能的空间:
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
Let me know if this respond to your question :)
如果这回应你的问题,请告诉我:)
#5
0
Use this:
用这个:
let aString: String = "This is my string"
let newString = aString.stringByReplacingOccurrencesOfString(" ", withString: "", options:[], range: nil)
print(newString)
Output : Thisismystring
输出:Thisismystring
#6
0
For Swift 4:
对于Swift 4:
let myString = "This \n is a st\tri\rng"
let trimmedString = myString.components(separatedBy: .whitespacesAndNewlines).joined()
#7
-1
Swift 4:
斯威夫特4:
let text = "This \n is a st\tri\rng"
let cleanedText = text.filter { !" \n\t\r".characters.contains($0) }
#1
37
Swift 4:
斯威夫特4:
let text = "This \n is a st\tri\rng"
let test = String(text.filter { !" \n\t\r".contains($0) })
Output:
输出:
print(test) // Thisisastring
While Fahri's answer is nice, I prefer it to be pure Swift ;)
虽然Fahri的答案很好,但我更喜欢纯粹的Swift;)
#2
6
You can use NSCharacterSet
whitespaceAndNewlineCharacterSet
to break up your string and use joinWithSeparator()
method to concatenate it back again as follow:
您可以使用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"
You can also extend String to make it easier for you to use it anywhere in your code:
您还可以扩展String以使您更容易在代码中的任何位置使用它:
extension String {
var removingWhitespacesAndNewlines: String {
return componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).joinWithSeparator("")
}
}
edit/update:
编辑/更新:
Swift3 or later
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"
#3
3
For the sake of completeness this is the Regular Expression version
为了完整起见,这是正则表达式版本
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"
#4
1
Suppose that you have this string : "some words \nanother word\n\r here something \tand something like \rmdjsbclsdcbsdilvb \n\rand finally this :)"
假设你有这个字符串:“有些单词\ nanother单词\ n \ r这里有东西\ t和类似\ rmdjsbclsdcbsdilvb \ n \ rand这样的东西:)”
here the how to remove all possible space :
这里有如何删除所有可能的空间:
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
Let me know if this respond to your question :)
如果这回应你的问题,请告诉我:)
#5
0
Use this:
用这个:
let aString: String = "This is my string"
let newString = aString.stringByReplacingOccurrencesOfString(" ", withString: "", options:[], range: nil)
print(newString)
Output : Thisismystring
输出:Thisismystring
#6
0
For Swift 4:
对于Swift 4:
let myString = "This \n is a st\tri\rng"
let trimmedString = myString.components(separatedBy: .whitespacesAndNewlines).joined()
#7
-1
Swift 4:
斯威夫特4:
let text = "This \n is a st\tri\rng"
let cleanedText = text.filter { !" \n\t\r".characters.contains($0) }