Filter non-digits from string

后端 未结 12 1127
日久生厌
日久生厌 2020-12-01 11:36

Using only swift code I cant figure out how to take \"(555) 555-5555\" and return only the numeric values and get \"5555555555\". I need to remove all the parentheses, whit

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 12:22

    I had a similar issue but I needed to retain the decimal points. I tweaked the top answer to this:

    extension String {
    
        /// Returns a string with all non-numeric characters removed
        public var numericString: String {
            let characterSet = CharacterSet(charactersIn: "0123456789.").inverted
            return components(separatedBy: characterSet)
                .joined()
        }
    }
    

提交回复
热议问题