如何在NSScanner的.txt文件中搜索字符串?

时间:2011-11-30 17:48:46

标签: iphone objective-c ios xcode nsscanner

  

可能重复:
  Find next match of a phrase with NSScanner

我目前有以下代码从UIWebView获取某段代码:

    NSURL *requestTimetableURL = [NSURL URLWithString:@"http://www.dhsb.org/index.phtml?d=201435"];
    NSLog(@"Loaded Timetable");
    NSError *error;
    NSString *page = [NSString stringWithContentsOfURL:requestTimetableURL 
                                              encoding:NSASCIIStringEncoding
                                                 error:&error];

    [webView loadHTMLString:page baseURL:requestTimetableURL];

    NSString* Period1;

    NSScanner *htmlScanner =  [NSScanner scannerWithString:page];

    [htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
    [htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
    [htmlScanner scanUpToString:@"</FONT>" intoString:&Period1];

如何扫描.txt文件(包含上面的字符串)代码?

E.G。 [htmlScanner scanUpToString:teststring intoString:NULL];

(teststring是.txt文件)

谢谢!

1 个答案:

答案 0 :(得分:0)

NSString * myText = [NSString stringWithContentsOfFile:path];

您可以在此处查看示例: http://iphoneincubator.com/blog/data-management/how-to-read-a-file-from-your-application-bundle

如果您不想从您的软件包(您的应用程序)中读取文件,请使用:

NSFileManager* fm = [NSFileManager defaultManager];
NSError* error=nil;
NSURL* docsurl = [fm URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
NSURL* fileUrl = [docsurl URLByAppendingPathComponent:@"test.txt"];
NSString *myText = [NSString stringWithContentsOfFile:fileUrl.path];