I have a string that contains words as well as a number. How can I extract that number from the string?
NSString *str = @\"This is my string. #1234\";
Self contained solution:
+ (NSString *)extractNumberFromText:(NSString *)text
{
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}
Handles the following cases:
Hope this helps!