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 😡
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++;
}
}
Your for loop will need to go along the length of the string. e.g.
Then you will want to get a single character from that string. Use the characterAtIndex method.
Then check for that character being between ‘0’ and ‘9’. e.g.
EDIT: Additional notes regarding the comments.
To count the numbers, start an integer at zero, before the for loop:
Inside the test for the character being numeric, increment it there:
Test for it being 28. Note the == for comparison:
EDIT: Additional notes for next comment