考虑将特殊字符放入 .csv 文件中

发布于 2024-11-07 12:38:32 字数 609 浏览 0 评论 0原文

我正在开发应用程序,在其中我必须考虑所有特殊字符。我正在使用下面的代码,但在其中我也想考虑“空格和逗号”,但在其中,当触发空格时,它会在该位置分割并创建新的字段为它..所以朋友们有没有考虑特殊字符的解决方案。如果您有任何想法,请建议我。谢谢

   arrCsv=[[NSArray alloc]initWithObjects:@"Hello",@"Hi",@"traun  testdata",@"Hi,fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName = [NSString stringWithFormat:@"%@/try.csv", documentsDirectory];


[[arrCsv componentsJoinedByString:@","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];

I am working on application, in it I have to consider all special characters.I am using below code but in it I also want to consider "space and comma" but in it, when trigger space it make split at that place and make new fild for it..so friends is there any solutions to consider special characters.If you have any idea then please suggest me.Thanks

   arrCsv=[[NSArray alloc]initWithObjects:@"Hello",@"Hi",@"traun  testdata",@"Hi,fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName = [NSString stringWithFormat:@"%@/try.csv", documentsDirectory];


[[arrCsv componentsJoinedByString:@","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2024-11-14 12:38:32

以简单的方式编写您自己的 CSV 解析例程既容易出错,又会浪费不必要的时间。我强烈建议您使用 Dave DeLong 优秀的 CHCSV 库。

如果您想使用此库,您可以按照以下方式执行操作以将两行写入文件:

NSArray *row1 = [NSArray arrayWithObjects:@"Field One", @"Field Two", @"Field, three", nil];
NSArray *row2 = [NSArray arrayWithObjects:@"Field' One", @"Field,, Two", @"Field\" three", nil];
NSArray *rows = [NSArray arrayWithObjects:row1, row2, nil];

NSError *error = nil;
[rows writeToCSVFile:@"path/to/file.csv" atomically:YES error:&error];
if (error) {
    // do something with error
}

Writing your own CSV parsing routines in a simplistic fashion is both error-prone and needlessly time-consuming. I highly recommend you use Dave DeLong's excellent CHCSV library.

If you wanted to use this library, you could do something along the following lines to write two rows to a file:

NSArray *row1 = [NSArray arrayWithObjects:@"Field One", @"Field Two", @"Field, three", nil];
NSArray *row2 = [NSArray arrayWithObjects:@"Field' One", @"Field,, Two", @"Field\" three", nil];
NSArray *rows = [NSArray arrayWithObjects:row1, row2, nil];

NSError *error = nil;
[rows writeToCSVFile:@"path/to/file.csv" atomically:YES error:&error];
if (error) {
    // do something with error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文