如何创建逗号分隔的字符串?

时间:2011-12-27 05:40:15

标签: iphone objective-c ios ipad ios4

我想创建一个逗号分隔的字符串。

NSString * list = @" iPhone,iPad,iPod&#34 ;;

我试过这样,

[strItemList appendString:[NSString stringWithFormat:@"%@,", [[arrItems objectAtIndex:i]objectForKey:@"ItemList"]]];

但问题是我得到了这样的字符串    @" iPhone,iPad,iPod," 请注意,还有一个额外的逗号","在字符串的末尾。我该如何避免使用额外的逗号?

你可以给我一个提示吗?非常感谢 提前致谢

8 个答案:

答案 0 :(得分:12)

要通过分隔符(字符串为字符串)将字符串数组连接成单个字符串,可以使用NSArray类的此方法:

NSArray* array = @[@"iPhone", @"iPad", @"iPod"];
NSString* query = [array componentsJoinedByString:@","];

通过使用此方法,您不需要删除最后一个额外的逗号(或其他),因为它不会将其添加到最终字符串。

答案 1 :(得分:4)

您可以选择几条路线。

如果项目的数量总是相同的,并且事先已知(我猜不是这种情况,但为了完整起见我提到它),只需立即制作整个字符串:

[NSString stringWithFormat:@"%@,%@,%@", [[arrItems objectAtIndex:0] objectForKey:@"ItemList"]], [[arrItems objectAtIndex:1] objectForKey:@"ItemList"]], [[arrItems objectAtIndex:2] objectForKey:@"ItemList"]]

知道不需要的逗号将始终是字符串中的最后一个字符,您可以在构造的最后一步删除它:

} // End of loop
[strItemList removeCharactersInRange:(NSRange){[strItemList length] - 1, 1}];

或者你可以稍微改变一下你的想法并按照这样做:

NSString * comma = @"";
for( i = 0; i < [arrItems count]; i++ ){

    [strItemList appendString:[NSString stringWithFormat:@"%@%@", comma, [[arrItems objectAtIndex:i]objectForKey:@"ItemList"]]];
    comma = @",";
}

请注意comma 之前另一项。在循环中设置该字符串意味着在第一个项目上不会添加任何内容,但每个其他项目都将添加一个逗号字符。

答案 2 :(得分:2)

完成循环后添加以下stmt

strItemList = [strItemList substringToIndex:[strItemList length]-1]

答案 3 :(得分:1)

如果数组计数是最后一个,则检查数组计数的值,然后在没有逗号的情况下添加,否则使用逗号添加。试试这个,我不太了解。

if([arrItems objectAtIndex:i] == arrItems.count){  
[strItemList appendString:[NSString stringWithFormat:@"%@", [[arrItems objectAtIndex:i]objectForKey:@"ItemList"]]];
}

else {
[strItemList appendString:[NSString stringWithFormat:@"%@,", [[arrItems objectAtIndex:i]objectForKey:@"ItemList"]]];

}

答案 4 :(得分:1)

假设arrItemsNSArray,其元素为@“iPhone”,@“iPad”和@“iPod”,您可以这样做:

NSArray *list = [arrItems componentsJoinedByString:@","]

答案 5 :(得分:0)

NSArray包含元素@“iPhone”,@“iPad”和@“iPod”

NSString *str=[[arrItems objectAtIndex:0]objectForKey:@"ItemList"]]

str =   [str stringByAppendingFormat:@",%@",[[arrItems objectAtIndex:1]objectForKey:@"ItemList"]]];
str =   [str stringByAppendingFormat:@",%@",[[arrItems objectAtIndex:2]objectForKey:@"ItemList"]]];
NsLog(@"%@",str);

答案 6 :(得分:0)

// Assuming...
NSDictionary *dictionary1 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"iPhone", @"iPodTouch", nil] forKey:@"ItemList"];
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"iPad", @"iPad2", @"Apple TV", nil] forKey:@"ItemList"];
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"iMac", @"MacBook Pro", @"Mac Pro", nil] forKey:@"ItemList"];
NSArray *arrItems = [NSArray arrayWithObjects:dictionary1, dictionary2, dictionary3, nil];

// create string list
NSString *strItemList = [[arrItems valueForKeyPath:@"@unionOfArrays.ItemList"] componentsJoinedByString:@", "];
NSLog(@"All Items List: %@", strItemList);

输出: 所有商品列表:iPhone,iPodTouch,iPad,iPad2,Apple TV,iMac,MacBook Pro,Mac Pro

答案 7 :(得分:0)

此方法将返回带有逗号分隔值的数据

的nsmutablestring

- (NSMutableString *)strMutableFromArray:(NSMutableArray *)arr withSeperater:(NSString *)saperator {     NSMutableString * strResult = [NSMutableString string];

for (int j=0; j<[arr count]; j++)
{
    NSString *strBar = [arr objectAtIndex:j];
    [strResult appendString:[NSString stringWithFormat:@"%@",strBar]];
    if (j != [arr count]-1)
    {
        [strResult appendString:[NSString stringWithFormat:@"%@",seperator]];
    }
}
return strResult;

}