Filter non-digits from string

后端 未结 12 1108
日久生厌
日久生厌 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:16

    Try this:

    let string = "(555) 555-5555"
    let digitString = string.filter { ("0"..."9").contains($0) }
    print(digitString) // 5555555555
    

    Putting in extension:

    extension String
    {
        var digitString: String { filter { ("0"..."9").contains($0) } }
    }
    
    print("(555) 555-5555".digitString) // 5555555555
    
    0 讨论(0)
  • 2020-12-01 12:17

    Swift 3 & 4:

    extension String {
        var digits: String {
            return components(separatedBy: CharacterSet.decimalDigits.inverted)
                .joined()
        }
    }
    
    0 讨论(0)
  • 2020-12-01 12:18

    You'll want to use NSCharacterSet:

    Check out this NSHipster link for Swift and Obj-C implementations: http://nshipster.com/nscharacterset/

    Similar example:

    var string = "  Lorem    ipsum dolar   sit  amet. "
    
    let components = string.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).filter({!isEmpty($0)})
    
    string = join(" ", components)
    

    See: punctuationCharacterSet

    Description:

    Returns a character set containing the characters in the category of Punctuation. Informally, this set is the set of all non-whitespace characters used to separate linguistic units in scripts, such as periods, dashes, parentheses, and so on.

    @Tapani Makes a great suggestion: NSCharacterSet.decimalDigitCharacterSet().invertedSet

    0 讨论(0)
  • 2020-12-01 12:20

    Not exactly answered but it looks like a number. I used URLComponents to build the url because it strips out parenthesis and dashes automatically:

    var telUrl: URL? {
        var component = URLComponents()
        component.scheme = "tel"
        component.path = "+49 (123) 1234 - 56789"
        return component.url
    }
    

    then

    UIApplication.shared.open(telUrl, options: [:], completionHandler: nil)
    

    calls +49 123 123456789

    0 讨论(0)
  • 2020-12-01 12:22

    In Swift 4 the solution is more nice:

    import Foundation
    
    let sourceText = "+5 (555) 555-5555"
    
    let allowedCharset = CharacterSet
        .decimalDigits
        .union(CharacterSet(charactersIn: "+"))
    
    let filteredText = String(sourceText.unicodeScalars.filter(allowedCharset.contains))
    
    print(filteredText) // +55555555555
    
    0 讨论(0)
  • 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()
        }
    }
    
    0 讨论(0)
提交回复
热议问题