Dwain
Dwain

Reputation: 53

How to count numbers in a string

i have a string with string length of 56.

First i tried to make sure, that the string hast the right length, if not, the app should fill several labels with other content.

if ([[NSString stringWithContentsOfURL:siteURL] length] == 56) {

    NSString *siteString = @"http://www.xxxx/iphone.txt";   
    NSURL *siteURL  = [NSURL URLWithString:siteString];
    myField2.text = [NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil]; 
    [NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil];
    NSArray *myArray = [[NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString: @":  "];


    [myField2 setText: [myArray objectAtIndex: 0]];
    [myField3 setText: [myArray objectAtIndex: 1]]; 
    [myField4 setText: [myArray objectAtIndex: 2]];
    [myField5 setText: [myArray objectAtIndex: 3]];
    [myField6 setText: [myArray objectAtIndex: 4]];


}
else {

    [myField2 setText: @"-"];
    [myField3 setText: @"-"];   
    [myField4 setText: @"-"];
    [myField5 setText: @"-"];
    [myField6 setText: @"-"];
}

In my string there are usually 28 numbers. I want my app to check the string, wether there are 28 numbers or not. I tried to use the following code:

for (int number = 0; number = 28; number++) {

}

But i guess that is not the right way.

I have a given range, 56, and possible numbers 0-9, any ideas how the code has to look like?

Thanks in advance :)

Well i tried it:

for (int number = 0; number < 56; number++){
    unichar c = [[NSString stringWithContentsOfURL:siteURL] characterAtIndex:number];

    if (c >= '0'  &&  c <= '9'){
        number++;

        if (number = 28) {
            NSString *siteString = @"http://www.xxxx/iphone.txt";   
            NSURL *siteURL  = [NSURL URLWithString:siteString];
            myField2.text = [NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil]; 
            [NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil];
            NSArray *myArray = [[NSString stringWithContentsOfURL:siteURL encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString: @":  "];


            [myField2 setText: [myArray objectAtIndex: 0]];
            [myField3 setText: [myArray objectAtIndex: 1]]; 
            [myField4 setText: [myArray objectAtIndex: 2]];
            [myField5 setText: [myArray objectAtIndex: 3]];
            [myField6 setText: [myArray objectAtIndex: 4]];
        }

        else {
            [myField2 setText: @"-"];
            [myField3 setText: @"-"];   
            [myField4 setText: @"-"];
            [myField5 setText: @"-"];
            [myField6 setText: @"-"];

        }

}
}

But it doesn't work, i guess i made some mistakes :x

current code:

int i = 0; 

for (int numberc = 0; numberc < 56; numberc++){ 
    unichar c = [[NSString stringWithContentsOfURL:siteURL encoding:NSASCIIStringEncoding error:&error] characterAtIndex:numberc]; 

    // Funktion die bestimmt, dass bei jedem positiven Match der Zähler um eins erhöht wird.    
    if (c >= '0'  &&  c <= '9'){ //Parameter für das Absuchen des Strings
        i++;
    }
}

Upvotes: 0

Views: 2524

Answers (4)

BGS
BGS

Reputation: 1441

how about this:

NSCharacterSet *digits=[NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSArray *tmpArray=[yourString componentsSeparatedByCharactersInSet:digits];
int nDigits=(int)tmpArray.count-1;

then do with nDigits whatever you want

Upvotes: 0

Dave
Dave

Reputation: 3448

Your for loop will need to go along the length of the string. e.g.

int nLength = [myString length];
for (int number = 0; number < nLength; number++)

Then you will want to get a single character from that string. Use the characterAtIndex method.

unichar c = [myString characterAtIndex:number];

Then check for that character being between '0' and '9'. e.g.

if (c >= '0'  &&  c <= '9')

EDIT: Additional notes regarding the comments. To count the numbers, start an integer at zero, before the for loop:

int numbercount = 0;

Inside the test for the character being numeric, increment it there:

if (c >= '0'  &&  c <= '9')
{
    numbercount++;

Test for it being 28. Note the == for comparison:

if (numbercount == 28)

EDIT: Additional notes for next comment

int i = 0;
NSString *url = [NSString stringWithContentsOfURL:siteURL encoding:NSASCIIStringEncoding error:&error];

for (int numberc = 0; numberc < 56; numberc++)
{
    unichar c =  [url characterAtIndex:numberc];

    // Funktion die bestimmt, dass bei jedem positiven Match der Zähler um eins erhöht wird.
    if (c >= '0'  &&  c <= '9')
    {
        //Parameter für das Absuchen des Strings         
        i++;
    }
} 

Upvotes: 3

Knodel
Knodel

Reputation: 4389

for (i=0; i<string.length; i++) { 
    if ([string characterAtIndex:i]=='0' || [string characterAtIndex:i]=='1' ... || [string characterAtIndex:i]=='9')
        numbercounter ++;
}

Hope this helps

Upvotes: 0

elp
elp

Reputation: 8131

for (i=0; i<string.length; i++) { 
    if ([[string characterAtIndex:i] intValue] <= 9 || [[string characterAtIndex:i] intValue] >= 0 )
        numbercounter ++;
}

Upvotes: 1

Related Questions