gpt4 book ai didi

ios - 删除前导逗号

转载 作者:行者123 更新时间:2023-12-01 19:14:05 27 4
gpt4 key购买 nike

我正在从sqlite数据库将信息读取到.csv文件。我能够建立文件,但是每行都以“,”开头。我已经做了一些android编程,但没有太多的ios。我认为问题出在componentsJoinedByString:@“,”,因为它把所有的东西都连接在一起,但是我不确定如何在每个新行的开头使用它来摆脱“,”。有没有其他方法可以将它们加入到csv文件中,或者可以摆脱第一个逗号?

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

最佳答案

更改

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


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

而不是 ",",请在 "\n"方法中使用 componentsJoinedByString,并在 addObject行中删除结尾的 "\n"

正如马丁在下面提到的,另一种选择是将换行符保留在 addObject行中,并使用 [exportBike componentsJoinedByString:@""]。这将在文件末尾添加一个换行符。

关于ios - 删除前导逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com