切掉长NSString的一部分 [英] Cut out a part of a long NSString

查看:76
本文介绍了切掉长NSString的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我想显示一个包含新闻的字符串。该字符串仅从免费网站加载,因此该网站的原始源代码不仅仅包含我的字符串,它更像是这样:

In my app I want to show a String that contains news. This string is being loaded just from a free Website, so the plain source code of the website does not contain only my string, its is more os less like this:

Stuff
More Stuff
More HTML Stuff
My String
More HTML Stuff
Final Stuff

当然,我想截断我不希望在NSString中使用的所有html内容。由于我要更改String fron的时间,因此网站上源代码的总长度会发生变化。这意味着 substringFromIndex 无法正常工作。还有其他方法可以将完整的源代码转换为我所需的字符串吗?

And of course i want to cut off all the html stuff that i don't want in my NSString. Since i am going to change the String fron time to time the overall length of the Source code from the website changes. This means that substringFromIndex wont work. Is there any other way to Convert the complete source code to just the String that i need?

推荐答案

有成千上万种方法可以处理文字。我将从正则表达式开始。如果您提供有关问题细节的更多详细信息,则可以获得更多具体帮助。

There are zillions of ways to manipulate text. I would start with regular expressions. If you give more details about the specifics of your problem, you can get more specific help.

编辑

感谢网站的链接。这给了我更多的工作空间。如果您始终知道所需内容的div的ID,则可以使用NSXMLParser提取div的文本。这会将NSTextField的文本设置为ID为 I3_sys_txt的div的内容。我在Mac上做到了,但我相信它也可以在iPhone上使用。

Thanks for the link to the website. That gives me more to work with. If you will always know the id of the div whose contents you want, you can use NSXMLParser to extract the text of the div. This will set the text of an NSTextField to the contents of the div with id "I3_sys_txt". I did this on the Mac but I believe it will work on the iPhone as well.

-(IBAction)buttonPressed:(id)sender {
    captureCharacters = NO;
    NSURL *theURL = [NSURL URLWithString:@"http://maxnerios.yolasite.com/"];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
    [parser setDelegate:self];
    [parser parse];
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqual:@"div"] && [[attributeDict objectForKey:@"id"] isEqual:@"I3_sys_txt"]) {
        captureCharacters = YES;
        divCharacters = [[NSMutableString alloc] initWithCapacity:500];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (captureCharacters) {
        //from parser:foundCharacters: docs:
        //The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. 
        //Because string may be only part of the total character content for the current element, you should append it to the current 
        //accumulation of characters until the element changes.
        [divCharacters appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if (captureCharacters) {
        captureCharacters = NO;
        [textField setStringValue:divCharacters];
        [divCharacters release];
    }
}

这篇关于切掉长NSString的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆