Pregunta

I have an NSString *fileName

This will contain a variable number from 1 to 3 digits. I want to extract all of the digits

I can get the first digit using

    //create text for appliance identifier
    char obsNumber = [fileName characterAtIndex:3];//get 4 character
    NSLog(@"Obs number %c",obsNumber);

    //Text label
    [cell.titleLabel setText:[NSString stringWithFormat:@"Item No: %c",obsNumber]];
    NSLog(@"Label for observation = %@",cell.titleLabel.text);

However if the string contains the number for example 78, or 204 I want to catch all two or three digits.

I tried this

 //create text for appliance identifier
    char obsNumber1 = [fileName characterAtIndex:3];//get 4 character
    char obsNumber2 = [fileName characterAtIndex:4];//get 5 character
    char obsNumber3 = [fileName characterAtIndex:5];//get 6 character

    NSLog(@"Obs number %c,%c,%c",obsNumber1,obsNumber2,obsNumber3);


    //Text label
    [cell.titleLabel setText:[NSString stringWithFormat:@"Item No: %c,%c,%c",obsNumber1,obsNumber2,obsNumber3]];
    NSLog(@"Label for observation = %@",cell.titleLabel.text);

This gave me 18c 1ce etc

¿Fue útil?

Solución 3

As your comment says , It has predefined set of values, right. Then try like this

NSstring *str = [filename substringFromIndex:11];
// Convert the str to char[]

Then you should try with the NSScanner :

NSString *numberString;

    NSScanner *scanner = [NSScanner scannerWithString:filename];
    NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

    // Throw away characters before the first number.
    [scanner scanUpToCharactersFromSet:numbers intoString:NULL];

    // Collect numbers.
    [scanner scanCharactersFromSet:numbers intoString:&numberString];

    // Result.
    int number = [numberString integerValue];

// you can play around with the set of number 

Otros consejos

Would this work for you?

NSString *filename = @"obs127observation"; //An example variable with your format

This code could be tidier but you should get the idea:

NSString *filenameNumber = [[filename
                             stringByReplacingOccurrencesOfString:@"observation"
                             withString:@""]
                            stringByReplacingOccurrencesOfString:@"obs"
                            withString:@""];

you can trim other letters except the decimals.

NSString *onlyNumbers=[yourstring stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];

Your approach of separating one character at a time and then combining them back into a string is an awkward, overly complex way of going about this. Kumar's suggestion of using NSScanner is a good option if you have a number in the middle of a string.

However, you make it sound like your string will always contain a number and only a number. Is that true? Or will there be characters you need to ignore?

You need to define the problem clearly and completely before you can select the best solution.

It might be as simple as using the NSString method substringWithRange.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top