2010-12-23 54 views
1
NSString *html="html page to parse"; 
NSString *text="some html text"; 

html = [html stringByReplacingOccurrencesOfString: 
      [NSString stringWithFormat:@"%@>", text] withString:@""]; 

我的问题是@"%@>"会在stringwithFormat中做什么。stringWithFormat是如何在这里使用的?

感谢

+0

[html stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] – GhostRider 2010-12-23 05:41:56

回答

1

代码

html = [html stringByReplacingOccurrencesOfString: 
      [NSString stringWithFormat:@"%@>", text] withString:@""]; 

将取代一些HTML文本>HTML页面空字符串解析 occurence。

所以结果将会是html页面只能解析

使用stringWithFormat您可以轻松地执行许多操作,例如一个int/float值转换为字符串,等等,

int age=18; 

NSSring *myage=[NSString stringWithFormat:@"My age is %d", age]; 

这里myage的值是我的年龄是18岁

2

%@告诉你NSString的将包括在你的字符串对象,所以它会尝试解析它作为一个字符串。根据Apple的说法,%@:

“Objective-C对象,作为由descriptionWithLocale返回的字符串打印:如果可用,或者说明不然,也适用于CFTypeRef对象,返回CFCopyDescription函数的结果。

第一个@符号只是表示一个NSString。

Apple documentation

+0

这样NSString将变成**`一些html文本> **。 – 2010-12-23 04:30:39

+0

啊是的。好的补充。 – Beaker 2010-12-23 05:45:32