Java在每个结束的html标记上子字符串(Java substring a string on every closing html markup)

编程入门 行业动态 更新时间:2024-10-26 18:18:05
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.

更多推荐

本文发布于:2023-04-28 00:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   标记   结束   在每个   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!