Jasonwilliams10
Jasonwilliams10

Reputation: 292

Delete leading comma

I am reading info from a sqlite database to a .csv file. I am able to get the file to build, but each new line starts with a ",". I have done some android programming, but not much ios. I am thinking that the issue is componentsJoinedByString:@"," since it joins everything together, but I am not sure how to get rid of the "," at the beginning of each new line using this. Is there another way to join them to write to a csv file or a way to get rid of the first comma?

NSMutableArray *exportBike = [[NSMutableArray alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docsPath = [paths objectAtIndex:0];

NSString *path = [docsPath stringByAppendingPathComponent:@"MemberDB.sql"];

FMDatabase *database = [FMDatabase databaseWithPath:path];

[database open];

FMResultSet *resultsBike = [database executeQuery:@"SELECT * FROM bike"];

while([resultsBike next]) {

    NSString *name = [resultsBike stringForColumn:@"name"];
    NSInteger stage1HR  = [resultsBike intForColumn:@"stage1HR"];
    NSInteger stage1BP = [resultsBike intForColumn:@"stage1BP"];
    NSInteger stage2HR = [resultsBike intForColumn:@"stage2HR"];
    NSInteger stage2BP = [resultsBike intForColumn:@"stage2BP"];
    NSInteger stage3HR  = [resultsBike intForColumn:@"stage3HR"];
    NSInteger stage3BP = [resultsBike intForColumn:@"stage3BP"];
    NSInteger stage4HR  = [resultsBike intForColumn:@"stage4HR"];
    NSInteger stage4BP = [resultsBike intForColumn:@"stage4BP"];
    NSInteger stage5HR  = [resultsBike intForColumn:@"stage5HR"];
    NSInteger stage5BP = [resultsBike intForColumn:@"stage5BP"];
    NSInteger stage6HR  = [resultsBike intForColumn:@"stage6HR"];
    NSInteger stage6BP = [resultsBike intForColumn:@"stage6BP"];
    NSInteger stage7HR  = [resultsBike intForColumn:@"stage7HR"];
    NSInteger stage7BP = [resultsBike intForColumn:@"stage7BP"];
    NSInteger recoveryHR = [resultsBike intForColumn:@"recoveryHR"];

    NSLog(@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR);

    [exportBike addObject:[NSString stringWithFormat:@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d""\n",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR]];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *savePath = [paths objectAtIndex:0];

    savePath = [savePath stringByAppendingPathComponent:@"bike.csv"];

    [[exportBike componentsJoinedByString:@","] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

Upvotes: 1

Views: 132

Answers (1)

iDev
iDev

Reputation: 23278

Change

[exportBike addObject:[NSString stringWithFormat:@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d""\n",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR]];

[[exportBike componentsJoinedByString:@","] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

to

[exportBike addObject:[NSString stringWithFormat:@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR]];

[[exportBike componentsJoinedByString:@"\n"] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

Instead of "," use "\n" in componentsJoinedByString method and in addObject line remove the "\n" at the end.

As Martin mentioned below, another option is to keep the new line character in addObject line and use [exportBike componentsJoinedByString:@""]. This will add a new line character at the end of the file.

Upvotes: 3

Related Questions