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

Iphone 如何创建逗号分隔的字符串?,iphone,objective-c,ios,ipad,ios4,Iphone,Objective C,Ios,Ipad,Ios4,我想创建一个逗号分隔的字符串,如下所示 NSString*list=@“iPhone、iPad、iPod” 我试着这样做 [strItemList appendString:[NSString stringWithFormat:@"%@,", [[arrItems objectAtIndex:i]objectForKey:@"ItemList"]]]; 但问题是我得到了这样的字符串 @“iPhone、iPad、iPod,”请注意,字符串末尾还有一个逗号“,”。我怎样才能避免那个多余的逗号呢 你

我想创建一个逗号分隔的字符串,如下所示

NSString*list=@“iPhone、iPad、iPod”

我试着这样做

[strItemList appendString:[NSString stringWithFormat:@"%@,", [[arrItems objectAtIndex:i]objectForKey:@"ItemList"]]];
但问题是我得到了这样的字符串 @“iPhone、iPad、iPod,”请注意,字符串末尾还有一个逗号“,”。我怎样才能避免那个多余的逗号呢

你能给我一个提示吗。高度赞赏
提前感谢

如果数组计数是最后一个,请检查数组计数的值,然后添加而不使用逗号,否则添加时使用逗号。试试这个,我不太清楚

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"]]];

}

您可以选择几条路线

如果项目的数量总是相同的,并且事先就知道了(我想不是这样,但为了完整起见,我提到了它),只需立即生成整个字符串:

[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 = @",";
}
NSString*逗号=@”;
对于(i=0;i<[ARR项目计数];i++){
[strItemList appendString:[NSString stringWithFormat:@“%@%@”,逗号,[[arrItems objectAtIndex:i]objectForKey:@“ItemList”]];
逗号=@“,”;
}

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

假设
arrItems
是一个
NSArray
,包含@“iPhone”、“iPad”和@“iPod”元素,则可以执行以下操作:

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

循环完成后,在stmt下方添加

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

要通过分隔符(字符即字符串)将字符串数组连接到单个字符串中,可以使用
NSArray
class的以下方法:

NSArray* array = @[@"iPhone", @"iPad", @"iPod"];
NSString* query = [array componentsJoinedByString:@","];
通过使用此方法,您不需要删除最后一个额外的逗号(或其他什么),因为它不会将其添加到最后一个字符串中。

n与元素@“iPhone”@“iPad”和@“iPod”对齐

输出:
所有项目列表:iPhone、iPodTouch、iPad、iPad2、苹果电视、iMac、MacBook Pro、Mac Pro

此方法将从数组中返回带有逗号分隔值的nsmutablestring

-(NSMutableString*)strMutableFromArray:(NSMutableArray*)arr与运算符:(NSString*)saperator { NSMutableString*strResult=[NSMutableString]

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;

for(int j=0;jYou可以使用substringToIndex optionsank感谢大家的支持。我能够解决它,再次感谢:):)这在技术上是可行的,但首选的习惯用法是componentsjoinedbythring。它更简洁,可能更快。@Scott根据问题,他有带字典的数组。根据要求,只有我给出了答案
// 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);
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;