2011-05-17 68 views
2

我正在處理應用程序,在其中我必須考慮所有特殊字符。我使用下面的代碼,但在其中我還想考慮「空格和逗號」,但在觸發時空間使其分裂在那個地方做出新的FILD朋友it..so有任何解決方案要考慮特殊characters.If你有任何想法,那麼請建議me.Thanks考慮將特殊的文字轉換爲.csv文件

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

你所說的「考慮」是什麼意思?你想通過空格和其他特殊字符分割字符串? – 2011-05-17 07:22:25

+0

@Terente Ionut Alexandru:我認爲他們希望能夠處理所有「特殊」字符,例如非字母數字。 – 2011-05-17 07:24:27

+0

嗨首先感謝rply ...我想考慮所有字符串之間的「」..means我有一個字符串@「你好,我好,」這整個字符串我想考慮1 fild.but當有觸發器空間或逗號它使另一個fild爲該 – sinh99 2011-05-17 07:34:42

回答

1

編寫自己的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 
} 
+0

感謝您的回覆,但請您詳細說明。 – sinh99 2011-05-18 07:56:39

+0

@ sinh99:我在我的回答中添加了一個示例用法的快速片段。 – 2011-05-18 11:13:10