在这段代码中我读了一个文件txt:这个txt文件有这样的信息: 一个#2#3#4#5;一个#2#3#4#5;
- (void) readFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"File.txt"];
NSString *fileString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
if (!fileString) {
NSLog(@"Error reading file.");
}
NSMutableArray *itemDB = [[fileString componentsSeparatedByString:@";"] mutableCopy];
[itemDB removeLastObject];
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
for (int i = 0; i<[itemDB count]; i++) {
NSArray *datiItemDB = [[itemDB objectAtIndex:i] componentsSeparatedByString:@"#"];
[returnArray addObject: datiItemDB];
}
one = [[NSMutableArray alloc] initWithArray:(NSArray*)returnArray];
[returnArray release];
}
此数组的nslog结果为
2011-07-04 10:01:16.847 Project[254:707] array one:(
(
"(\n 0,\n 1,\n 12,\n \"2011-07-04 08:00:41 +0000\",\n \"2011-07-15 00:00:00 +0000\",\n \"\",\n 0,\n 0,\n (\n ),\n \"\"\n)"
)
)
但我希望nslog的结果是
2011-07-04 09:52:07.281 Project[230:707] array one:(
(
0,
1,
1,
"2011-07-04 07:45:15 +0000",
"2011-07-04 07:45:15 +0000",
"",
0,
0,
(
),
""
)
)
为什么我有“/ n”符号,如何从数组中删除它?
答案 0 :(得分:0)
您是否只想将文本文件的第一部分放在数组中?我认为你的问题太复杂了。这样的事情怎么样:
NSString *inputString = @"one#two#three#four#five;one1#two2#three3#four4#five5;";
NSArray *arrayFromInputString = [inputString componentsSeparatedByString:@";"];
NSArray *component1 = [[arrayFromInputString objectAtIndex:0] componentsSeparatedByString:@"#"];
NSLog(@"1: %@", [component1 description]);
NSArray *component2 = [[arrayFromInputString objectAtIndex:1] componentsSeparatedByString:@"#"];
NSLog(@"2: %@", [component2 description]);
如果你运行它,输出是
[777:207] 1: (
one,
two,
three,
four,
five
)
[777:207] 2: (
one1,
two2,
three3,
four4,
five5
)
这是你想要的吗?如果没有,你能尝试更好地解释吗?