[英]Why is there is missing “\n” character when I convert NSString to NSdata?
[英]I am able to get the CSV file data into array,but when it is parsing the next line, it is taking a \n character. how to remove the \n character
我能夠將CSV文件數據放入數組,但是當它接收下一行的數據時,它使用\\n
字符。 如何刪除\\n
字符。
我的CSV文件的內容是:
你好,世界,嗯
nnn,sss,eee
我得到的輸出為
2011-08-24 15:29:16.069 CSVParsing[1030:903] (
Hello
)
2011-08-24 15:29:16.072 CSVParsing[1030:903] (
World
)
2011-08-24 15:29:16.075 CSVParsing[1030:903] (
"hhh\nnnn"
)
2011-08-24 15:29:16.076 CSVParsing[1030:903] (
sss
)
2011-08-24 15:29:16.077 CSVParsing[1030:903] (
"eee\n"
)
如何克服這個問題
以下是我正在使用的代碼。
{
NSString * pathToFile = [[NSBundle mainBundle] pathForResource:@“ hw” ofType:@“ csv”]; NSError *錯誤;
NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:&error];
if (!fileString) {
NSLog(@"Error reading file.");
}
scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@","]];
while ([scanner scanUpToString:@"," intoString:&pathToFile] )
{
arrayItems = [pathToFile componentsSeparatedByString:@","];
NSLog(@"%@",arrayItems);
}
}
您可以使用
NSMutableString * string = @"hhh\nnnn";
NSRange foundRange = [string rangeOfString:@"\n"];
if (foundRange.location != NSNotFound)
{
[string replaceCharactersInRange:foundRange withString:@""];
}
一種方法是:首先將文件分成幾行:
// lineArray is a NSArray
// filedata is a NSString containing the full file
self.lineArray = [filedata componentsSeparatedByString:@"\n"];
您只需掃描行尾,將換行符視為另一個字符。 您可以通過將掃描儀代碼包裝到enumerateLinesUsingBlock :(這是一個NSString方法)中來解決此問題。
但是實際上,這還不是一個完整的解析器。 您可能必須處理很多特殊情況。 也許您應該四處看看,例如:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.