2016-01-22 176 views

回答

30

斯威夫特4:

let text = "This \n is a st\tri\rng" 
let test = String(text.filter { !" \n\t\r".contains($0) }) 

输出:

print(test) // Thisisastring 

虽然法赫里的回答是不错的,我喜欢它是纯粹的雨燕;)

+0

我想'filter'是这个太贵了,这有什么错'removeSubrange'或'stringByTrimmingCharacters'等? – kakubei

+0

@kakubei:removeSubrange意味着你必须搜索整个字符串的子范围,在进行任何删除操作之前,已经像过滤一样昂贵。 stringByTrimmingCharacters只在字符串的开始和结尾修剪,而不在字的中间。 –

0

使用此:

let aString: String = "This is my string" 
let newString = aString.stringByReplacingOccurrencesOfString(" ", withString: "", options:[], range: nil) 
print(newString) 

输出: Thisismystring

5

您可以使用NSCharacterSetwhitespaceAndNewlineCharacterSet分手您的字符串,并使用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" 
1

假设你有这样的字符串:“有些话这里\字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 

让我知道这对你的问题:)

-1

斯威夫特4回应:

let text = "This \n is a st\tri\rng" 
let cleanedText = text.filter { !" \n\t\r".characters.contains($0) } 
1

为了完整起见,这是正则表达式版本

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" 
+0

谢谢,这对我有用。 另外我想用一个空格替换所有连续的\ 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

0

的SWIFT 4:

let myString = "This \n is a st\tri\rng" 
let trimmedString = myString.components(separatedBy: .whitespacesAndNewlines).joined() 
+0

已经有一个完美的解决方案。 –

+0

过滤器太昂贵,我认为对于初学者来说,这将更有用..这就是为什么我加了 – 2018-02-27 11:19:46

+0

好吧,如果你认为那样就没问题。但请继续并请解释您的解决方案。 –

相关问题