[英]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.