2012-12-14 4 views
0

내가 배열의 각 항목은 예를 <this is column 1><this is column 2> 등을 위해> < 사이에 문자열을 구문 분석하려고 ....의 Obj-C는 : 항목은 <>

이 도움말이 많이 주시면 감사하겠습니다에있는 문자열 배열 만들기 .

감사

+0

이 포함되어있는 경우 해당 코드가 작동하지 않습니다. 다시 확인하고 답변을 수락하거나 후속 질문을 게시하는 것은 어떻습니까? – Mario

답변

1
NSString *input [email protected]"<one><two><three>"; 
NSString *strippedInput = [input stringByReplacingOccurencesOfString: @">" withString: @""]; //strips all > from input string 
NSArray *array = [strippedInput componentsSeperatedByString:@"<"]; 

[배열은 objectAtIndex : 0] 참고 빈 문자열 ("")가 t를 것 "실제"문자열 중 하나에 < 또는>

+0

+1 나는 똑같이 생각하고 있었다 –

+0

많은 감사, 미안 나는 휴일 휴가를 통해 오프라인되었습니다. – James

1

한 가지 방법은 또는 이있는 NSString에서을 componentsSeparatedByString componentsSeparatedByCharactersInSet를 사용하도록 할 수 있습니다.

NSString *test = @"<one> <two> <three>"; 

NSArray *array1 = [test componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 

NSArray *array2 = [test componentsSeparatedByString:@"<"]; 

당신은 몇 가지가 이후에 청소, 하나 보여 배열 2 또는 하는 array1

2

뭔가의 경우 공백 문자열을 제거하는 경우 트리밍 수행해야합니다 :

NSString *string = @"<this is column 1><this is column 2>"; 
NSScanner *scanner = [NSScanner scannerWithString:string]; 

NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; 

NSString *temp; 

while ([scanner isAtEnd] == NO) 
{ 
    // Disregard the result of the scanner because it returns NO if the 
    // "up to" string is the first one it encounters. 
    // You should still have this in case there are other characters 
    // between the right and left angle brackets. 
    (void) [scanner scanUpToString:@"<" intoString:NULL]; 

    // Scan the left angle bracket to move the scanner location past it. 
    (void) [scanner scanString:@"<" intoString:NULL]; 

    // Attempt to get the string. 
    BOOL success = [scanner scanUpToString:@">" intoString:&temp]; 

    // Scan the right angle bracket to move the scanner location past it. 
    (void) [scanner scanString:@">" intoString:NULL]; 

    if (success == YES) 
    { 
     [array addObject:temp]; 
    } 
} 

NSLog(@"%@", array); 
관련 문제