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

发布于 2024-12-18 17:28:04 字数 1252 浏览 0 评论 0原文

可能的重复:
使用 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 文件(包含上面的字符串)以获取代码?

EG [htmlScanner scanUpToString:teststring intoString:NULL];

(teststring 是 .txt 文件)

谢谢!

Possible Duplicate:
Find next match of a phrase with NSScanner

I currently have the following code to get a certain piece of code from the 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];

How can I scan a .txt file (which contains the string like above) for the code?

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

(teststring being the .txt file)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2024-12-25 17:28:04

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

NSString *myText = [NSString stringWithContentsOfFile:path];

You can see examples here:
http://iphoneincubator.com/blog/data-management/how-to-read-a-file-from-your-application-bundle

if you don't want to read from your bundle (your app) but from your documents dir, use:

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文