I am trying to remove all of the non-numeric characters from an NSString
, but I also need to keep the spaces. Here is what I have been using.
NS
// Our test string
NSString* _bbox = @"Test 333 9599 999";
// Remove everything except numeric digits and spaces
NSString *strippedBbox = [_bbox stringByReplacingOccurrencesOfString:@"[^\\d ]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [_bbox length])];
// (Optional) Trim spaces on either end, but keep spaces in the middle
strippedBbox = [strippedBbox stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// Print result
NSLog(@"%@", strippedBbox);
This prints 333 9599 999
, which I think is what you're after. It also removes non numeric characters that may be in the middle of the string, such as parentheses.