删除NSString中的所有非数字字符,保留空格
我试图从NSString
删除所有的非数字字符,但我也需要保持空格。 这是我一直在使用的。
NSString *strippedBbox = [_bbox stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [_bbox length])];
如果我给它一个NSString的Test 333 9599 999
它将返回3339599999
但我需要保持空间。
我怎样才能做到这一点?
通过创build你想要保留的字符的字符集并使用invertedSet
来创build一个“所有其他”集合,很容易完成。 然后将该string拆分成一个由该集合中的任何字符分隔的数组,然后重新组合string。 听起来很复杂但很简单:
NSCharacterSet *setToRemove = [NSCharacterSet characterSetWithCharactersInString:@"0123456789 "]; NSCharacterSet *setToKeep = [setToRemove invertedSet]; NSString *newString = [[someString componentsSeparatedByCharactersInSet:setToKeep] componentsJoinedByString:@""];
结果: 333 9599 99
在你可以改变你的第一个正则expression式包含9:
在迅速:
var str = "test Test 333 9599 999"; val strippedStr = str.stringByReplacingOccurrencesOfString("[^0-9 ]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range:nil); // strippedStr = " 33 9599 999"
虽然这留下了领先的空间,你可以应用一个空白修剪来处理:
strippedStr.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) // strippedStr = "33 9599 999"
// 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);
这打印333 9599 999
,我认为这是你以后。 它还删除可能位于string中间的非数字字符,例如括号。
对于Swift 3.0.1的人
var str = "1 3 6 .599.188-99 " str.replacingOccurrences(of: "[^0-9]", with: "", options: .regularExpression, range: nil)
输出: "13659918899"
这也从string修剪空格
尝试使用NSScanner
NSString *originalString = @"(123) 123123 abc"; NSMutableString *strippedString = [NSMutableString stringWithCapacity:originalString.length]; NSScanner *scanner = [NSScanner scannerWithString:originalString]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789 "]; while ([scanner isAtEnd] == NO) { NSString *buffer; if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) { [strippedString appendString:buffer]; } else { [scanner setScanLocation:([scanner scanLocation] + 1)]; } } NSLog(@"%@", strippedString); // "123123123"
NSMutableString strippedBbox = [_bbox mutableCopy]; NSCharacterSet* charSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890 "].invertedSet; NSUInteger start = 0; NSUInteger length = _bbox.length; while(length > 0) { NSRange range = [strippedBbox rangeOfCharacterFromSet:charSet options:0 range:NSMakeRange(start, length)]; if(range.location == NSNotFound) { break; } start += (range.location + range.length); length -= range.length; [strippedBbox replaceCharactersInRange:range withString:@""]; }
简而言之,你可以使用NSCharacterSet来检查那些你感兴趣的字符,而忽略其他字符。
- (void) stripper { NSString *inString = @"A1 B2 C3 D4"; NSString *outString = @""; for (int i = 0; i < inString.length; i++) { if ([[NSCharacterSet whitespaceCharacterSet] characterIsMember:[inString characterAtIndex:i]] || [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[inString characterAtIndex:i]]) { outString = [outString stringByAppendingString:[NSString stringWithFormat:@"%c",[inString characterAtIndex:i]]]; } } }