How to remove all the spaces and \n\r in a String?

前端 未结 10 1272
耶瑟儿~
耶瑟儿~ 2020-12-28 12:44

What is the most efficient way to remove all the spaces, \\n and \\r in a String in Swift?

I have tried:

for character in s         


        
10条回答
  •  梦谈多话
    2020-12-28 13:05

    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"
    

提交回复
热议问题