Reputation: 6324
`Hi, i have a string with a 2 digit numbers (like 32) but i want to get and use it as 2 single digit number (like 3 and 2). how can I do it? thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
unichar firstChar = [firstThird characterAtIndex: 0];
NSString *firstCharStr = [NSString stringWithFormat:@"%c", firstChar];
unichar secChar = [firstThird characterAtIndex: 1];
NSString *secCharStr = [NSString stringWithFormat:@"%c",secChar];
NSLog(@"%@ over %@", firstCharStr, secCharStr);
if ([firstCharStr isEqualToString: @"1"]) {
firstCharStr=@"wet";
NSLog(@"wet");
}
if ([firstCharStr isEqualToString: @"2"]) {
firstCharStr=@"snow";
NSLog(@"snow");
}
if ([firstCharStr isEqualToString: @"3"]) {
firstCharStr=@"ice";
NSLog(@"ice");
}
if ([secCharStr isEqualToString: @"1"]) {
secCharStr=@"wet";
NSLog(@"wet");
}
if ([secCharStr isEqualToString: @"2"]) {
secCharStr=@"snow";
NSLog(@"snow");
}
if ([secCharStr isEqualToString: @"3"]) {
secCharStr=@"ice";
NSLog(@"ice");
}
NSString *secThird = [components objectAtIndex:1];
if ([secThird isEqualToString: @"1"]) {
secThird = @"wet";
NSLog(@"wet");
}
if ([secThird isEqualToString:@"2"]) {
secThird = @"snow";
NSLog(@"snow");
}
if ([secThird isEqualToString:@"3"]) {
secThird = @"ice";
NSLog(@"ice");
}
NSString *thirdThird = [components objectAtIndex:2];
if ([thirdThird isEqualToString: @"1"]) {
thirdThird = @"wet";
NSLog(@"wet");
}
if ([thirdThird isEqualToString:@"2"]) {
thirdThird = @"snow";
NSLog(@"snow");
}
if ([thirdThird isEqualToString:@"3"]) {
thirdThird = @"ice";
NSLog(@"ice");
}
dep.text = [NSString stringWithFormat:@"1/3 %@%@ 2/3 %@ 3/3 %@", firstCharStr,secCharStr, secThird, thirdThird];
it works now with to digits (ie 32) but if i try to put just one digit I get an error.... any idea? thanks
Upvotes: 1
Views: 794
Reputation: 55334
NSString *firstThird = [components objectAtIndex:0];//--- I get 32 from this string but i want 3 and 2
NSArray *numbersAsStrings = [firstThird componentsSeparatedByString:@""];
NSMutableArray *digits = [NSMutableArray array];
for (NSString *digit in numbersAsStrings)
{
[digits addObject:[NSNumber numberWithInt:[digit intValue]]];
}
This sample can take any number and convert it into an array of NSNumber
s.
Access:
int int1 = [[digits objectAtIndex:0]intValue]; //returns 3
Upvotes: 0
Reputation: 3352
NSString's -characterAtIndex:
should do the trick:
NSString *firstThird = @"32";
unichar c1, c2;
c1 = [ firstThird characterAtIndex: 0 ];
c2 = [ firstThird characterAtIndex: 1 ];
switch ( c1 ) {
case '3':
...
}
Upvotes: 2
Reputation: 8513
Skipping any unicode complexities, how about extracting the characters
unichar firstChar = [firstThird characterAtIndex: 0];
Upvotes: 0
Reputation: 789
Loop through and extract one character at a time from your string.
for(int i = 0; i < [myString length]; i++)
{
char extractedChar = [myString characterAtIndex:i];
if(extractedChar == '1')....
}
Upvotes: 1