Skip to content

Instantly share code, notes, and snippets.

@shannoga
Created May 30, 2011 12:41
Show Gist options
  • Save shannoga/998856 to your computer and use it in GitHub Desktop.
Save shannoga/998856 to your computer and use it in GitHub Desktop.
Get only numbers from NSString
+(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;
}
@Vjaya3sb
Copy link

Vjaya3sb commented Jul 4, 2014

cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment