2011-05-13 9 views
1

안녕하세요, 저는 2 자릿수 (32와 같은)의 문자열을 가지고 있습니다. 그러나 나는 그것을 얻고 싶습니다. 3과 2 같은 2 자리 숫자로 사용하고 싶습니다. 내가 어떻게 해? 미리 감사드립니다.NSString - 2 자리 숫자

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"]) { 
    [email protected]"wet"; 
    NSLog(@"wet"); 
} 
if ([firstCharStr isEqualToString: @"2"]) { 
    [email protected]"snow"; 

    NSLog(@"snow"); 
} 
if ([firstCharStr isEqualToString: @"3"]) { 
    [email protected]"ice"; 
    NSLog(@"ice"); 
    } 

if ([secCharStr isEqualToString: @"1"]) { 
    [email protected]"wet"; 
    NSLog(@"wet"); 
} 
if ([secCharStr isEqualToString: @"2"]) { 
    [email protected]"snow"; 

    NSLog(@"snow"); 
} 
if ([secCharStr isEqualToString: @"3"]) { 
    [email protected]"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]; 

이제는 숫자 (예 : 32)로 작동하지만 한 자리 만 입력하면 오류가 발생합니다 .... 어떤 생각입니까? 감사합니다

+1

확인 문자열이 실제로 첫번째 숫자에 불과 ... [당신이 구성 요소 부분으로는 NSString 분할 어떻게, 엑스 코드를 사용]의 – bbum

+0

가능한 중복 (포함하지 않는다 http://stackoverflow.com/questions/3942263/using-xcode-how-do-you-split-nsstring-into-component-parts) –

+0

@ Josh Caswell 고마워, 나는 시도 할 것이다 ... – mat

답변

1

문자열을 한 번에 한 문자 씩 반복하여 추출하십시오.

for(int i = 0; i < [myString length]; i++) 
{ 
     char extractedChar = [myString characterAtIndex:i]; 

     if(extractedChar == '1').... 
} 
0

유니 복잡성 스킵 어떻게 문자

추출에 대한 firstChar = unichar에 [firstThird characterAtIndex : 0];

2

있는 NSString의 -characterAtIndex: 트릭해야한다 :이 샘플은 임의의 숫자를 가지고 NSNumber의 배열로 변환 할 수 있습니다

NSString   *firstThird = @"32"; 
unichar    c1, c2; 

c1 = [ firstThird characterAtIndex: 0 ]; 
c2 = [ firstThird characterAtIndex: 1 ]; 

switch (c1) { 
case '3': 
    ... 
} 
0
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]]]; 
} 

합니다.

액세스 : 당신은 확인하십시오 무엇 이건

int int1 = [[digits objectAtIndex:0]intValue]; //returns 3 
관련 문제