将每个项添加到字符串中

时间:2010-10-06 08:52:19

标签: iphone objective-c nsstring

    //Parse this shit
//Create array of all items in order (with submatches still
NSString *myregex1 = @"\\<([a-z0-9]+)\\sref\=\"([^\"]*)\">([^\<]*)\\<\\/\\1\>";
//Get all items in an array
NSArray *items = [stringReply componentsMatchedByRegex:myregex1];
//Create string to hold all items in
NSString *AllOrderItems;

if ([items count] > 0) {

    for (NSString *item in items) {
        //NSLog(@"%d", i );
        NSString *ref = [item stringByMatching:myregex1 capture:2];
        NSString *value = [item stringByMatching:myregex1 capture:3];
        NSLog(@"Current Item: %@ : %@", ref, value);
        AllOrderItems = [NSString stringWithFormat:(@"%@%@: %@\n", AllOrderItems, ref, value)];
        OrderDetails.text = AllOrderItems;
    }
}

我想要获得每个参考资料值为字符串AllOrderItems所以我可以在textView

中显示它

由于

:)

3 个答案:

答案 0 :(得分:1)

AllOrderItems开头是零。

然后创建一个新字符串,其中AllOrderItems的值为其中一个部分,即nil。所以它分配,零,参考,价值。然后你再次这样做,所以你得到nil,ref,value,nil,ref value。等等。

答案 1 :(得分:0)

也许使用NSArray的componentsJoinedByString

答案 2 :(得分:0)

我认为你想要的是:

//Parse this shit
//Create array of all items in order (with submatches still
NSString *myregex1 = @"\\<([a-z0-9]+)\\sref\=\"([^\"]*)\">([^\<]*)\\<\\/\\1\>";
//Get all items in an array
NSArray *items = [stringReply componentsMatchedByRegex:myregex1];
//Create string to hold all items in
NSString *allOrderItems = @"";  // Intentionally existing but empty string!

if ([items count] > 0) {
    for (NSString *item in items) {
        //NSLog(@"%d", i );
        NSString *ref = [item stringByMatching:myregex1 capture:2];
        NSString *value = [item stringByMatching:myregex1 capture:3];
        NSLog(@"Current Item: %@ : %@", ref, value);
        allOrderItems = [allOrderItems stringByAppendingFormat:(@"%@: %@\n", ref, value)];
    }
    orderDetails.text = AllOrderItems;
}