Created
May 30, 2011 12:41
-
-
Save shannoga/998856 to your computer and use it in GitHub Desktop.
Get only numbers from NSString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(NSString*)getOnlyNumbersFromString:(NSString*)stringToStrip{ | |
NSMutableString *strippedString = [NSMutableString | |
stringWithCapacity:stringToStrip.length]; | |
NSScanner *scanner = [NSScanner scannerWithString:stringToStrip]; | |
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)]; | |
} | |
} | |
return strippedString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool :)