[英]Android Search in string and “Trim”
所以我得到了一個很大的弦,然后我開始尋找
"<div style=\"font-size:15px;\"><b>"
和
"</b></div>"
就像這樣:(實際上是IF語句。)
htmlCode.contains("<div style=\"font-size:15px;\"><b>")
&& htmlCode.contains("</b></div>")
無論如何,在第一個"<div style=\\"font-size:15px;\\"><b>"
之后是文本,而在文本之后是第二個文本。 ( "</b></div>"
)
現在,我要“獲取”該文本,並最終刪除為其他文本,而僅保留“中間文本”。
我已經搜索了一段時間,找不到解決方案。
如果您可以向我發布文檔鏈接或示例,那將是很好的。
額外的信息:
/*
* 1. Search for "<div style="font-size:15px;"><b>" ** TITLE COMES HERE **
* 2. Search for "</b></div>" ** This is after the Title, so Title is between 1 and 2.
* 3. Search for "<div style="float:left;"><a href="" ** Link Comes Here **
* 4. Search for "" rel="nofollow" target="_blank" style="color:green;">" ** Same as 2. // Link Instead **
*
* -- How it should do it --
* 1 -> Wait -> 2 -> Wait -> Get the text between 1 and 2 -> Save in String (Array) -> 3 -> Wait -> 4 -> Wait
* -> Get the text between 3 and 4 -> Save in String (Array) -> Repeat Process (Max 25(?)).
*/
完整字符串的示例為:
<div style="font-size:15px;"><b>**My Little Pony.**</b></div>
(加星號的文字是我想要得到的)
提前致謝!
您需要找到第一段htmlCode.indexOf(“”)的索引,並使用htmlCode.substring(int start,int length)查找各段之間的文本。
int index1 = htmlCode.indexOf("<div style=\"font-size:15px;\"><b>");
int index2 = htmlCode.indexOf("</b></div>");
String textInsideDiv = htmlCode.substring(index1+"<div style=\"font-size:15px;\"><b>".length(), index2);
您應該使用Java regexps,在這里查看:
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
6.4。 構建鏈接檢查器
下面是進行這種提取的樣品
import java.util.*;
import java.lang.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Rextester
{
public static void main(String args[])
{
Pattern htmltag = Pattern.compile("<div style=\"font-size:15px;\"><b>(.*?)</b></div>");
String subjectString = "<div style=\"font-size:15px;\"><b>**My Little Pony.**</b></div>";
Matcher tagmatch = htmltag.matcher(subjectString);
while (tagmatch.find()) {
System.out.println(tagmatch.group(1));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.