0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AndroidでHTMLコードの変換時のあれこれのメモ

Last updated at Posted at 2024-06-09

HTMLコードの変換(装飾)

androidのAPI Lvevl が23以下なら

Html.FromHtml(text)

androidのAPI Lvevl が24からは ↑の方法は非推奨とされているので、 HtmlCompatを使う

HtmlCompat.FromHtml(text, HtmlCompat.FromHtmlModeCompact)

正規表現で一部のHTMLタグを非表示にする

imageタグを非表示にする

text.replaceAll("<img [^>]*>", "")

正規表現で一部のHTMLタグを他の文字に変換する

text.replaceAll("<video [^>]*>", "ビデオ").replaceAll("</video>", "")

正規表現で特定のタグがいくつあるか数える

  private fun countTags(string: String): Int {
        val pattern = "<img [^>]*>"
        val pattern2 = "<video [^>]*>"
        
        val matcherImage = Pattern.compile(pattern).matcher(string)
        val matcherVideo = Pattern.compile(pattern2).matcher(string)
        
        var count = 0
        while (matcherImage.find()) {
            count++
        }
        while (matcherVideo.find()) {
            count++
        }
        return count
    }

タグにこだわらなくても、特定の文字列の数を数えられる...

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?