4

i have a string which has both numeric and character value.

Like: string = abc1234

Now I want to get only the integer part from it:

i.e. 1234

How can I do this? I have tried the following with no luck:

  NSString *str = @"abc123";
  int s = [str intValue];
Reece Kenney
  • 2,734
  • 3
  • 24
  • 57
ios
  • 955
  • 1
  • 12
  • 35

1 Answers1

4

Use this function

- (NSString *)extractNumberFromText:(NSString *)text
{
  NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
  return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}

It will help you.Thankyou

baydi
  • 1,003
  • 6
  • 11