0

I want to create a comma-separated string like this.

NSString *list = @"iPhone,iPad,iPod";

I tried like this,

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

But the issue is I'm getting a string like this @"iPhone,iPad,iPod," Note that there is an extra comma "," at the end of the string. How can I avoid that extra comma?

Can you please give me a hint. Highly appreciated Thanks in advance

1
  • You can use substringToIndex option Commented Dec 27, 2011 at 5:48

8 Answers 8

12

To join an array of strings into a single string by a separator (character which would be a string), you could use this method of NSArray class:

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

By using this method, you won't need to drop the last extra comma (or whatever) because it won't add it to the final string.

Sign up to request clarification or add additional context in comments.

Comments

4

There's a couple of routes you can take.

If the number of items is always the same, and known before hand (which I guess isn't the case, but I mention it for completeness's sake), just make the whole string at once:

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

Knowing that the unwanted comma will always be the last character in the string, you can make removing it the last step in construction:

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

Or you can change your thinking a little and do the loop like this:

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

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

Notice that comma comes before the other item. Setting that string inside the loop means that nothing will be added on the first item, but a comma character will be for every other item.

Comments

2

After Completion of loop add below stmt

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

2 Comments

This technically works, but the preferred idiom is componentsJoinedByString. it's more concise and potentially faster.
@Scott as per question he have array with Dictionary.as per requirement only i given answer
1

check the value of array count if array count is last then add without comma else add with comma. try this out i am not sure to much about.

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

}

1 Comment

Thanks all of you for the support. I was able to solve it, thanks again :) :)
1

Assuming that arrItems is an NSArray with elements @"iPhone", @"iPad", and @"iPod", you can do this:

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

Comments

0

NSArray with elements @"iPhone", @"iPad", and @"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);

Comments

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);

Output: All Items List: iPhone, iPodTouch, iPad, iPad2, Apple TV, iMac, MacBook Pro, Mac Pro

Comments

0

This method will return you the nsmutablestring with comma separated values from an array

-(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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.