1

可能重复:
从 iPhone 上的 NSString 中删除 HTML 标签

我正在使用谷歌方向 api 在我的地图视图中显示地图,它给了我 HTML 方向字符串,其中包含 HTML 标记。

现在我想以纯文本形式显示该字符串我该怎么做。我的字符串在这里:

    Head <b>southwest</b> toward <b>GH Rd</b>
    Exit the roundabout onto <b>GH Rd</b><div style="font-size:0.9em">Go through 1 roundabout</div>
    At the roundabout, take the <b>1st</b> exit onto <b>Road Number 2</b><div style="font-size:0.9em">Pass by myonlinesearch.blogspot.com (on the left in 600&nbsp;m)</div>
    At the roundabout, take the <b>3rd</b> exit onto <b>CH Rd</b>
    At <b>Indroda Cir</b>, take the <b>2nd</b> exit onto <b>Gandhinagar Ahmedabad Rd/SH 71</b><div style="font-size:0.9em">Continue to follow Gandhinagar Ahmedabad Rd</div><div style="font-size:0.9em">Go through 1 roundabout</div>
    At the roundabout, take the <b>1st</b> exit onto <b>Sardar Patel Ring Rd</b>
    At <b>Ranasan Cir</b>, take the <b>3rd</b> exit onto <b>NH 8</b><div style="font-size:0.9em">Pass by Galaxy Restaurant (on the left in 4.3&nbsp;km)</div>
    Turn <b>left</b> onto <b>Galaxy Rd</b><div style="font-size:0.9em">Pass by Shiv Shakti Food Fort (on the left)</div>
    Turn <b>left</b> onto <b>NH 59</b>
    Turn <b>right</b><div style="font-size:0.9em">Go through 1 roundabout</div>
    Turn <b>right</b>
   Turn <b>left</b><div style="font-size:0.9em">Destination will be on the right</div>
4

3 回答 3

3

您可以使用正则表达式/谓词来删除 < & > 之间的所有字符。

但是如果您的文本包含一些 <> 它将被删除

  NSRange range;
  NSString *string;
  while ((range = [string rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
    string=[string stringByReplacingCharactersInRange:range withString:@""];
  }
  NSLog(@"Un block string : %@",string);
于 2013-01-22T08:49:48.200 回答
0

您将不得不通过解析器解析文本,解析这些文本非常容易,只需扫描行并在那里使用替换功能。

 [yourString stringByReplacingOccurrencesOfString:@"<b>"withString:""];
[yourString stringByReplacingOccurrencesOfString:@"</b>"withString:""];

这将为您解决问题。

于 2013-01-22T08:51:45.967 回答
0

要么你可以去..

-(NSString *) stringByStrippingHTML
{
  NSRange r;
  NSString *s = [[自我复制] autorelease];
  while ((r = [s rangeOfString:@"]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:@""];
  返回 s;
}

或者更好地使用 NSString 类别从您的字符串中删除 HTML,即“GTMNSStringHTMLAdditions”。

于 2013-01-22T08:57:04.047 回答