3

Is it possible to extract a number from a string. For example, I have a string:

my name is shishir and my number is 98890876478

Can I extract on 98890876478 from the above string?

Or if my string is:

my name is shishir and my number is XXX98890876478XXX

In this condition can I extract `98890876478", which is in between of XXX.

Is it possible to do this?

I am getting a message from a server which should be in the format as above, and I need the numeric value for further operations

Edit:

Here's the code I'm trying to use:

NSString *logString = [NSString stringWithFormat:@"%@",theXML]; 
NSString *digits = [logString stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]; 
NSLog(@"Message id: %i", [digits intValue]);

which outputs:

2010-05-21 16:37:07.092 sms[5311:207] OK: message-ID XXX110103468XXX
2010-05-21 16:37:07.851 sms[5311:207] Message id: 2

I think its returning two because size==2. I need to retrieve the value between the "XXX".

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100

5 Answers5

7

I think what you need is NSScanner, this allows you to take out parts of the string.

e.g.

int n;
NSScanner * scanner = [[NSScanner alloc] initWithString:@"Your string 1234"];
[scanner scanInt:&n];
AndersK
  • 35,813
  • 6
  • 60
  • 86
1

Self contained solution:

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

Handles the following cases:

  • @"1234"@"1234"
  • @"001234"@"001234"
  • @"leading text get removed 001234"@"001234"
  • @"001234 trailing text gets removed"@"001234"
  • @"a0b0c1d2e3f4"@"001234"

Hope this helps!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
1

You can use regex for this: you want to match \d+, that is, a non-zero sequence of digits.

Related questions


Otherwise, you can just manually go through the characters in the string and find any contiguous sequence of digits.

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • But watch out for users with digits in their usernames... Better look for the "my number is" string first, and start scanning from there. – David Gelhar May 21 '10 at 10:38
0

stringByTrimmingCharactersInSet just remove character at the end or beginning of a string.

ex.: "abc 10 cd" -> 10 (it will work)

ex.: "abc 10 a 1" -> "10 a 1" (it will not work)

Using regex seem a better idea.

iphone sdk - Remove all characters except for numbers 0-9 from a string

EDIT:

Check not the accepted answer but the one with more than 50 votes

NSString * number = @"(555) 555-555 Office";
NSString * strippedNumber = [number stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [number length])];
Community
  • 1
  • 1
Hugues BR
  • 2,238
  • 1
  • 20
  • 26
0

THIS IS WHAT I FINALLY DONE WITH

NSString *newString = [[theXML componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
----------------------------------------

NSString *newStr = [newString substringWithRange:NSMakeRange(1, [newString length]-1)];
----------------------------------------

And I am done.

Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100