簡體   English   中英

入門 <img> 從Android中的html字符串

[英]Getting <img> from html string in Android

我有一個html字符串。 我想從標簽中提取src屬性。 我在“ summaryContent”中得到html字符串,現在我希望它finf並返回src。 如果此字符串包含兩個或三個標簽,則應找到它的所有“ src”。

for (int i = 0; i < contents.size(); i++) {
                if (contents.get(i).summary != null) {
                    summaryContent = contents.get(i).summary; // There is only one time this condition is true
                } else {
                    continue;
                }

這是我在summaryContent中得到的

<ol start="7">
<li>
<h3><strong>Charlotte Casiraghi</strong></h3>
</li>
</ol>
<strong>Family Fortune:  </strong>$1 billion
<img class="size-full wp-image-346 aligncenter" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/Picture1.png" alt="Picture1" width="943" height="1350" />
&nbsp;
&nbsp;
Charlotte Marie Pomeline Casiraghi is the second child of Caroline Princess of Hanover, Princess of Monaco and Stefano Casiraghi, an industrialist. She is eight in line to the throne of Monaco. Charlotte is a published writer and magazine editor.
<img class="aligncenter" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/f762a5ca08aab85785f48c8425f089d7.png" alt="" />
Charlotte and her two brothers were born in the Mediterranean Principality of Monaco. When she was four years old, her father was killed in a boating accident. After his death, Princess Caroline moved the family to the Midi village of Saint-Rémy-de-Provence in France, with the intention of minimizing their exposure to the press.
<!--nextpage-->
<ol start="6">
<li>
<h3><strong>Hind Hariri</strong></h3>
</li>
</ol>

您可以使用正則表達式將其提取:

Pattern p = Pattern.compile("src\\s*=\\s*['\"]([^'\"]+)['\"]");
Matcher m = p.matcher(summaryContent);
if (m.find()) {
  String srcResult = m.group(1);
}

說明

  • src從字面上匹配字符src(區分大小寫)

  • \\s*匹配任何空格字符[\\ r \\ n \\ t \\ f]

  • Quantifier: *在0到無限制的時間之間,盡可能多的次數,並根據需要返回[貪婪]

  • =匹配的字符=字面上

  • \\s*匹配任何空格字符[\\ r \\ n \\ t \\ f]

  • Quantifier: *在0到無限制的時間之間,盡可能多的次數,並根據需要返回[貪婪]

  • ['"]匹配下面列表中出現的單個字符

  • '"列表中的單個字符“”,按字面值(區分大小寫)

  • 1st Capturing group ([^'"]+)匹配下面列表中不存在的單個字符

  • Quantifier: +一次至無限次,盡可能多次,並根據需要返回[貪婪]

  • '"列表中的單個字符“”,按字面值(區分大小寫)

  • ['"]匹配下面列表中出現的單個字符

  • '"列表中的單個字符“”,按字面值(區分大小寫)

我建議探索使用正則表達式的可能性。

您可以從這里閱讀: 從HTML標記獲取屬性的正則表達式

您可以使用subString方法從htmlString中提取src標記。

htmlString = htmlString.substring(htmlString.indexOf("src=\""));
htmlString = htmlString.substring("src=\"".length());
htmlString = htmlString.substring(0, htmlString.indexOf("\""));

希望這可以幫助。

說明

第1步:

  • htmlString.indexOf(“ src = \\”“)

這將找到實際字符串中遇到“ src”標簽的索引位置。

  • htmlString.substring(htmlString.indexOf(“ src = \\”“))

然后,從找到的“ src”標簽的索引位置開始對原始字符串進行子字符串化。

第2步:

  • htmlString.substring(“ src = \\”“。length())

在這里,我們從步驟1獲得的字符串中刪除“ src”標簽。

最后一步

  • htmlString.substring(0,htmlString.indexOf(“ \\”“))

從第零個索引開始到下一個雙引號出現,我們對字符串進行子字符串提取以在src標記中提取鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM