1

重複の可能性:
iPhone で NSString から HTML タグを削除する

Google方向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

どちらでも行くことができます。

-(NSString *)stringByStrippingHTML
{{
  NSRange r;
  NSString * s=[[セルフコピー]自動リリース];
  while((r = [s rangeOfString:@ "] +>" options:NSRegularExpressionSearch])。location!= NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:@ ""];
  戻り値;
}

または、NSStringカテゴリを使用して、文字列からHTMLを削除することをお勧めします(例: "GTMNSStringHTMLAdditions")。

于 2013-01-22T08:57:04.047 に答える
0

パーサーを介してテキストを解析する必要があります。これらのテキストを解析するのは非常に簡単です。行をスキャンしてそこで置換機能を使用するだけです。

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

これでうまくいきます。

于 2013-01-22T08:51:45.967 に答える