1

How do I get string using NSScanner from a string which contains string as well as numbers too?

i.e. 001234852ACDSB

The result should be 001234852 and ACDSB

I am able to get numbers from the string using NSScanner and characters by using stringByReplacingOccurrencesOfString but I want to know, is that possible to get string from with the use of NSScanner or any other built in methods?

I would like to know the Regex for the same.

alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
  • 3
    It's unclear what you're asking... you want to use an NSScanner to get a string from a string? Are you trying to remove all the numbers and just get the letters? Please give an example of sample input and its expected output. – BJ Homer Jul 30 '11 at 05:27
  • It seems he wants to separate the digits from the letters and get a string with digits and one with letters as result. Alhough he doesn't say so, I assume the numbers are together in one part of the string and the letters in the rest of it, or they alternate. – Rudy Velthuis Jul 30 '11 at 05:50

3 Answers3

2

If you can guarantee that the string always consists of numbers followed by letters, then you could do the following with NSScanner:

NSScanner *scanner = [NSScanner scannerWithString:@"001234852ACDSB"];

NSString *theNumbers = nil;

[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
                    intoString:&theNumbers];

NSString *theLetters = nil;
[scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet]
                    intoString:&theLetters];

A regular expression capturing the same things would look like this:

([0-9]+)([a-zA-Z]+)
BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • Thanks Sir. This code is partially helpful to me, because it is not at all guaranteed about the placing of numbers and letters. – alloc_iNit Jul 30 '11 at 06:02
2

Finally after google for the same and go through some information from net, I reached to my destination. With this I'm posting the code, this may help many who are facing the same problem as I have.

    NSString *str = @"001234852ACDSB";
    NSScanner *scanner = [NSScanner scannerWithString:str];

    // set it to skip non-numeric characters
    [scanner setCharactersToBeSkipped:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];

    int i;
    while ([scanner scanInt:&i])
    {
        NSLog(@"Found int: %d",i);    //001234852
    }

    // reset the scanner to skip numeric characters
    [scanner setScanLocation:0];
    [scanner setCharactersToBeSkipped:[NSCharacterSet decimalDigitCharacterSet]];

    NSString *resultString;
    while ([scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&resultString])
    {
        NSLog(@"Found string: %@",resultString);    //ACDSB
    }
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
0

You don't have to use a scanner to do it.

NSString *mixedString = @"01223abcdsadf";

NSString *numbers = [[mixedString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];

NSString *characters = [[mixedString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnouprstuwvxyz"] invertedSet]] componentsJoinedByString:@""];

For other possible solution view this question Remove all but numbers from NSString

Community
  • 1
  • 1
Cyprian
  • 9,423
  • 4
  • 39
  • 73