我想一個字符串解析到一個數組中的元素是<之間>例如<this is column 1><this is column 2>
等....的OBJ-C:創建陣列從字符串中的項目在<>
幫助將不勝感激。
感謝
我想一個字符串解析到一個數組中的元素是<之間>例如<this is column 1><this is column 2>
等....的OBJ-C:創建陣列從字符串中的項目在<>
幫助將不勝感激。
感謝
NSString *input [email protected]"<one><two><three>";
NSString *strippedInput = [input stringByReplacingOccurencesOfString: @">" withString: @""]; //strips all > from input string
NSArray *array = [strippedInput componentsSeperatedByString:@"<"];
注意,[陣列objectAtIndex:0]將是一個空串( 「」)的噸他當然不會工作,如果其中一個「實際」字符串包含<或>
+1我在想同一個 –
非常感謝,對不起,我在節假日休假時已經離線了。 – James
一種方法可能是使用兩種componentsSeparatedByCharactersInSet或從NSString的componentsSeparatedByString。
NSString *test = @"<one> <two> <three>";
NSArray *array1 = [test componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSArray *array2 = [test componentsSeparatedByString:@"<"];
你需要做一些清理之後,無論是在數組2或陣列1
東西的情況下,去除空白的字符串的情況下,微調證明:
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);
如何檢查並可能接受答案或發佈後續問題......? – Mario