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

前端 未结 10 1253
耶瑟儿~
耶瑟儿~ 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:06

    If anyone is wondering why, despite of putting "\n" and "\r" into the set, "\r\n" is not removed from the string, it's because "\r\n" is treated by swift as one character.

    Swift 4:

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

    "\n" is not duplicated by accident

提交回复
热议问题