Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:50:43+00:00 2026-06-11T16:50:43+00:00

What is the easiest and best way in Objective-C to combine a list (NSArray)

  • 0

What is the easiest and best way in Objective-C to combine a list (NSArray) of NSStrings into a single NSString separated by commas, with the grammatically correct terminal conjunction “, and ” before the final item of the list?

NSArray *anArray = [NSArray arrayWithObjects:@"milk", @"butter", @"eggs", @"spam", nil];

From this array, I want the NSString @"milk, butter, eggs, and spam".

More generally, if the list is more than two items long, I want ", " between every item except the last and second-to-last (which should have ", and "). If the list is two items long, I want just the ' and ' with no comma. If the list is one item long, I want the single string from the array.

I like something as simple as:

NSString *newString = [anArray componentsJoinedByString:@", "];

But this of course omits the ‘and’ conjunction.

Is there a simpler and/or faster Objective-C way than the following:

- (NSString *)grammaticallyCorrectStringFromArrayOfStrings:(NSArray *)anArray {
    if (anArray == nil) return nil;
    int arrayCount = [anArray count];
    if (arrayCount == 0) return @"";
    if (arrayCount == 1) return [anArray objectAtIndex:0];
    if (arrayCount == 2) return  [anArray componentsJoinedByString:@" and "];

    // arrayCount > 2
    NSString *newString = @"";
    for (NSString *thisString in anArray) {
        if (thisString != [anArray objectAtIndex:0] && thisString != [anArray lastObject]) {
            newString = [newString stringByAppendingString:@", "];
        }
        else if (thisString == [anArray lastObject]) {
            newString = [newString stringByAppendingString:@", and "];
        }
        newString = [newString stringByAppendingString:thisString];
    }
    return newString;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:50:44+00:00Added an answer on June 11, 2026 at 4:50 pm

    For the loop, I’d probably do something like

    NSMutableString *newString = [NSMutableString string];
    NSUInteger lastIndex = arrayCount - 1;
    [anArray enumerateObjectsUsingBlock:^(NSString *thisString, NSUInteger idx, BOOL *stop) {
        if (idx != 0)
            [newString appendString:@","];
        if (idx == lastIndex)
            [newString appendString:@" and "];
    
        [newString appendString:thisString];
    }];
    

    Though I guess that’s not really less lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did some research into the easiest/best way to generate a pdf in my
What is the best/easiest way to sort a large list of words (10,000-20,000) by
The best way to remove duplicate values ( NSString ) from NSMutableArray in Objective-C?
What's the best/easiest way to shuffle a long list in Mathematica?
What is the best/easiest way to store a list (LinkedList) of a class which
Whats the best/easiest way to test for administrative rights in a PowerShell script? I
What is the best/easiest way to build a minimal task queue system for Linux
I'd like to know the best/easiest way to get a visual diff of a
In ANSI C on Windows what is the best/easiest way to transfer data between
Can anyone please recommend the best and easiest way to include multiple Javascript files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.