2011-04-05 6 views
0

"1234 56789 1235 98765 ..."(네 자리 - 공백 - 다섯 자리) 루프를 세 번 반복합니다. 빈 공간이없는 경우가 있습니다. 이렇게 : "1234 56789 123598765 ..."하지만 구분 4와 5 자리는 여전히 관련이 있습니다.NSString을 2D로 조작 NSMutableArray

그리고 나는 어떻게 잘라내어 각 줄의 내용을 데이터 구조와 같은 테이블에 붙여 넣는 지에 대해 의아해하고 있습니다. 이 할 수있는 현명한 방법이 있나요 ... 내가 더 나아가 다른 6 개 자리를 잡아하려고이 후

for (int column = 0; column < 6; column++) { 
     // take first 4 digits 
     cursor_offset += 4; 
     temp = [entry substringWithRange:NSMakeRange(cursor,cursor_offset)]; 
     cursor = cursor_offset; // update cursor position 
     if ([entry substringWithRange:NSMakeRange(cursor_offset,cursor_offset+1)] isEqualToString:@" "]) { 
      // space jump 
      cursor_offset+=1; // identify blank space and jump over it 
     } 

:이 내가 지금까지 가지고 무엇인가? 나는 정규식을 생각하지 않았고, 그다지 신경 쓰지 않았다. 모범 사례?

답변

1

나는 아마 조각에서 모든 문자열에서 공백 후 덩어리를 제거하는 것 :

NSString *source = @"1234 56789 1234 1234 56789 1234 1234 56789 1234 1234 56789 1234 1234 56789 1234"; 
NSString *stripped = [[source componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet] componentsJoinedByString:@""]; 

NSAssert([stripped length] % 13 == 0, @"string length must be a multiple of 13"); 

NSMutableArray *sections = [NSMutableArray array]; 
for (NSInteger location = 0; location < [stripped length]; location += 13) { 
    NSString *substring = [stripped substringWithRange:NSMakeRange(location, 13)]; 
    NSArray *fields = [NSArray arrayWithObjects: 
        [substring substringWithRange:NSMakeRange(0,4)], 
        [substring substringWithRange:NSMakeRange(4,5)], 
        [substring substringWithRange:NSMakeRange(9,4)], 
        nil]; 
    [sections addObject:fields]; 
} 

경고, 브라우저에서 입력은 컴파일되지. 주의 사항 구현 자

+0

우아한. 이게 서투른 for-loop보다 훨씬 더 똑똑합니다 ... 고마워요. –