Using regex in Swift

前端 未结 2 2076
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 04:02

I\'m trying to replace the occurrences of all spaces and special characters within a string in Swift.

I don\'t know what the RegEx would be in Swift.

I am tr

2条回答
  •  迷失自我
    2021-01-07 04:42

    Additionally, you could extend String and accomplish the same bit. But if it's homework and they said function stick with that.

    var word = "Hello World!!!"
    
    extension String {
        func regEx() -> String {
            return self.replacingOccurrences(of: "\\W+", with: "", options: .regularExpression, range: nil)
        }
    }
    
    word.regEx()
    

提交回复
热议问题