2016-01-22 262 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

好吧,如果你認爲那樣就沒問題。但請繼續並請解釋您的解決方案。 –

相關問題