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 8879531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:58:12+00:00 2026-06-14T19:58:12+00:00

I need to save NSStrings from an array into a .csv file in my

  • 0

I need to save NSStrings from an array into a .csv file in my documents directory, which is not a problem, but I don’t know how to handle the carriage return.. I specify that I want to apply a carriage return every 8 cells and this happens correctly. However when it comes to write the actual CSV file an extra unwanted comma is added after the carriage return! This is the incriminated line:

NSString *ArrayConvertedIntoStringFINAL = [FinalStringsArrayWithCarriageReturns componentsJoinedByString:@","];

How can I tell the compiler not to apply a comma after a carriage return?

Here is the complete code. Thanks guys!

//Add a Carriage Return symbol every N objects (every FREQUENCY)
int FREQUENCY = 8;

//Define String WithCarriageReturns
NSMutableArray *FinalStringsArrayWithCarriageReturns = [[NSMutableArray alloc] initWithCapacity:0];

for (int i = 1; i <= [FinalStringsArray count]; i++)
{
    NSString *readValue = [FinalStringsArray objectAtIndex:i-1];
    //Add objects to the final string
    [FinalStringsArrayWithCarriageReturns addObject:readValue];

    if ( (i % FREQUENCY) == 0) //Number is multiple of N
    {
        //Every multiple of N add the Carriage Return symbol
        [FinalStringsArrayWithCarriageReturns addObject:CarriageReturn];
    }
}
NSLog(@"+2+Broken down array strings: %@",FinalStringsArrayWithCarriageReturns);

//PROBLEM! A comma is added after the Carriage Return

//Convert Array into String
NSString *ArrayConvertedIntoStringFINAL = [FinalStringsArrayWithCarriageReturns componentsJoinedByString:@","];
  • 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-14T19:58:14+00:00Added an answer on June 14, 2026 at 7:58 pm

    The approach you have posted results in 2 additional copies of every string. That’s a lot of wasted memory. Here’s a simpler approach that only requires a single extra copy and no extra arrays:

    int FREQUENCY = 8;
    
    NSMutableString *csv = [NSMutableString string];
    
    NSUInteger count = FinalStringsArray.count;
    for (int i = 1; i <= count; i++) {
        NSString *readValue = FinalStringsArray[i - 1];
    
        // TODO - deal with possibility of 'readValue' containing a comma or carriage return
        [csv appendString:readValue];
    
        if ((i % FREQUENCY) == 0) { //Number is multiple of N
            [csv appendString:CarriageReturn];
        } else {
            [csv appendString:@","];
        }
    }
    

    Depending on how much data you need to write, it would be even better to write directly to a file instead of the mutable string. This would result in no extra copy of the data and would be faster than writing to the mutable string first.

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

Sidebar

Related Questions

I need to save NSStrings from an array into a .txt file in my
I need to save all .xml file names in a directory to a vector.
In a document based application I am able to save/read from a file, but
.txt file need save in SD-card. Save(n + String.valueOf(key) + .txt); private void Save(String
I need to save some classes and data structures to a file. My first
I need to save an camera image from my application to the desktop so
I know how to create plist file but I need to delete plist file.
i need to download a mp3 file from internet through my application and i
I am developing an application in which I am getting a XML file from
I need to download and save videos from video sites like youtube. I want

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.