How to use stringByAddingPercentEncodingWithAllowedCharacters() for a URL in Swift 2.0

For the given URL string the equivalent to let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) is the character set URLQueryAllowedCharacterSet let urlwithPercentEscapes = myurlstring.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet()) Swift 3: let urlwithPercentEscapes = myurlstring.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed) It encodes everything after the question mark in the URL string. Since the method stringByAddingPercentEncodingWithAllowedCharacters can return nil, use optional bindings as suggested in the … Read more

Strip Non-Alphanumeric Characters from an NSString

We can do this by splitting and then joining. Requires OS X 10.5+ for the componentsSeparatedByCharactersInSet: NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; NSString *strippedReplacement = [[someString componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@””];

Remove all non-numeric characters from a string in swift

I was hoping there would be something like stringFromCharactersInSet() which would allow me to specify only valid characters to keep. You can either use trimmingCharacters with the inverted character set to remove characters from the start or the end of the string. In Swift 3 and later: let result = string.trimmingCharacters(in: CharacterSet(charactersIn: “0123456789.”).inverted) Or, if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)