Java在每个结束的html标记上子字符串(Java substring a string on every closing html markup)
我有一个包含HTML标记的字符串,如下所示:
<b> Hi </b> i'm a <i> beautifull </i> <u> string </u>我需要在每个html结束标记之后拆分字符串,并在变量和文本中获取标记,如下所示:
startMarkup: <b> text: Hi endMarkup: </b> startMarkup: text: i'm endMarkup: startMarkup: <i> text: beautifull endMarkup: </i> startMarkup: <font size="5"> text: string endMarkup: </font>请建议一个很好的算法来实现这一目标。
I have a string which contains HTML markup like the below:
<b> Hi </b> i'm a <i> beautifull </i> <u> string </u>I need to split the string after each html closing markup and get the markup in a variable and text in another variable like the below:
startMarkup: <b> text: Hi endMarkup: </b> startMarkup: text: i'm endMarkup: startMarkup: <i> text: beautifull endMarkup: </i> startMarkup: <font size="5"> text: string endMarkup: </font>Please suggest a good algorithm to achieve this.
最满意答案
尝试这个
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { List<String> startmarkups = new ArrayList<>(); List<String> endmarkups = new ArrayList<>(); List<String> texts = new ArrayList<>(); String s1 = "<b> Hi </b> i'm a <i> beautifull </i> <u> string </u>"; //Get startmarkup and endmarkups into respective array String mk[] = s1.split(">"); for(int i = 0; i < mk.length; i++){ System.out.println(mk[i]); if(!mk[i].trim().startsWith("<")){ if(mk[i].indexOf("<") >= 0){ if(mk[i].indexOf("/") >= 0){ endmarkups.add("</"+(mk[i].split("/")[1])+">"); startmarkups.add("<"+(mk[i].split("<")[1])+">"); }else{ endmarkups.add(""); startmarkups.add(""); } } } } //Get text into texts array for(int i = 0; i < mk.length; i++){ if(!mk[i].trim().startsWith("<")){ if(mk[i].indexOf("<") >= 0) texts.add((mk[i].split("<")[0])); } } for(int i = 0; i < startmarkups.size(); i++) { System.out.print("Startmarkup: " + startmarkups.get(i) + "\t"); System.out.print("Text: " + texts.get(i) + "\t"); System.out.print("Endmarkup: " + endmarkups.get(i) + "\t"); System.out.println(); } } }用html字符串替换s1变量。
Try this
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { List<String> startmarkups = new ArrayList<>(); List<String> endmarkups = new ArrayList<>(); List<String> texts = new ArrayList<>(); String s1 = "<b> Hi </b> i'm a <i> beautifull </i> <u> string </u>"; //Get startmarkup and endmarkups into respective array String mk[] = s1.split(">"); for(int i = 0; i < mk.length; i++){ System.out.println(mk[i]); if(!mk[i].trim().startsWith("<")){ if(mk[i].indexOf("<") >= 0){ if(mk[i].indexOf("/") >= 0){ endmarkups.add("</"+(mk[i].split("/")[1])+">"); startmarkups.add("<"+(mk[i].split("<")[1])+">"); }else{ endmarkups.add(""); startmarkups.add(""); } } } } //Get text into texts array for(int i = 0; i < mk.length; i++){ if(!mk[i].trim().startsWith("<")){ if(mk[i].indexOf("<") >= 0) texts.add((mk[i].split("<")[0])); } } for(int i = 0; i < startmarkups.size(); i++) { System.out.print("Startmarkup: " + startmarkups.get(i) + "\t"); System.out.print("Text: " + texts.get(i) + "\t"); System.out.print("Endmarkup: " + endmarkups.get(i) + "\t"); System.out.println(); } } }Replace the s1 variable with the html string.
更多推荐
发布评论