Demand:
Handle a piece of HTML code <p> tags so that it is not nested, closed, and no content is exposed outside the label
For example:
11111<p></p>22222<p>33333</p><p>44444</p>555555<p></p>66666
Mind Mapping:
Code:
public static string CHECKPP (string content) {string str;int time =0;int pos =0;int path =0;boolean flag =true;int index1 = 0;int Index2 =0;//Ensure that the positions are <p> and </p>int firstindex = Content.indexof ("<p>"), if (FirstIndex!=0) { Content = "<p>" +content;} int lastindex = Content.lastindexof ("</p>"); if (lastindex! = Content.length ()-4 | | lastindex = = 1) {content = Conten T + "</p>";} StringBuffer sb = new StringBuffer (content);d o{time++; System.out.println ("-------" +time+ "-----Path:" +path); switch (path) {case 0:index1 =sb.indexof ("<p>", pos+1); Index2 =sb.indexof ("</p>", pos+1); System.out.println (Sb.tostring ()); System.out.println ("POS:" +pos); System.out.println ("index1:" +index1); System.out.println ("Index2:" +index2); if (index1! =-1) {if (INDEX2<INDEX1) {//<p></p>path = 1;pos = Index2;} else{//<p> (...) *<p>→<p> (XXX) *</P><P>SB = Sb.insert (index1, "</p>");p ath = 0;pos = index1+4;}} else{/No More <p> buT it ' s not end yet:<p> (...</p>) *</p>path = 1;pos = Index2;} Break;case 1:index1 =sb.indexof ("<p>", pos+1); Index2 =sb.indexof ("</p>", pos+1); System.out.println (Sb.tostring ()); System.out.println ("POS:" +pos); System.out.println ("index1:" +index1); System.out.println ("Index2:" +index2);//no more </p> exitif (index2 = =-1) {flag = False;break;} No more <p> but not endif (index1 = =-1) {SB = Sb.insert (pos+4, "<p>");p ath =0;pos = pos + 7;break;} else{/* </p> (...) *<p> *↑* POS */if (INDEX1<INDEX2) {if (Index1-pos > 4) {//</p> (XXX) <p>→</p><p> (xx x) </P><P>SB = Sb.insert (pos+4, "<p>"), SB = Sb.insert (index1+3, "</p>");p ath = 0;pos = Index1 +7;} else if (Index1-pos = = 4) {//</p><p> path = 0;pos = Index1;}} else{//</p> (...) *</p>→</p><p> (...) *</P>SB = Sb.insert (pos+4, "<p>");p ath = 1;pos = Index2 +3;} break;} Default:break;} System.out.println ("POS:" +pos); System.out.println (Sb.tostring ());//exitif (pos = = Sb.length ()-4) break;} while (flag); Str =sb.tostring (); Str= Str.replaceall ("<p> (\\s) * ( ) * () *</p>", ""); return Str;}
Log of the execution example:
11111<p></p>22222<p>33333</p><p>44444</p>555555<p></p>66666--- ----1-----path:0<p>11111<p></p>22222<p>33333</p><p>44444</p>555555 <p></p>66666</p>pos:0index1:8index2:11pos:12<p>11111</p><p></p>22222 <p>33333</p><p>44444</p>555555<p></p>66666</p>-------2-----path:0 <p>11111</p><p></p>22222<p>33333</p><p>44444</p>555555<p ></p>66666</p>pos:12index1:24index2:15pos:15<p>11111</p><p></p>22222 <p>33333</p><p>44444</p>555555<p></p>66666</p>-------3-----path:1 <p>11111</p><p></p>22222<p>33333</p><p>44444</p>555555<p ></p>66666</p>pos:15index1:24index2:32pos:31<p>11111</p><p></p><p >22222</p><p>33333</p><p>44444</p>555555<p></p>66666</p>-------4-----path:0<p> 11111</p><p></p><p>22222</p><p>33333</p><p>44444</p> 555555<p></p>66666</p>pos:31index1:43index2:39pos:39<p>11111</p><p></p ><p>22222</p><p>33333</p><p>44444</p>555555<p></p>66666< /p>-------5-----path:1<p>11111</p><p></p><p>22222</p><p>33333< /p><p>44444</p>555555<p></p>66666</p>pos:39index1:43index2:51pos:43<p> 11111</p><p></p><p>22222</p><p>33333</p><p>44444</p> 555555<p></p>66666</p>-------6-----path:0<p>11111</p><p></p><p >22222</p><p>33333</p><p>44444</p>555555<p></p>66666</p>pos : 43index1:61index2:51pos:51<p>11111</p><p></p><p>22222</p><p>33333</p><p>44444 </p>555555<p></p>66666</p>-------7-----path:1<p>11111</p><p></p ><p>22222</p><p>33333</p><p>44444</p>555555<p></p>66666< /p>pos:51index1:61index2:64pos:68<p>11111</p><p></p><p>22222</p><p >33333</p><p>44444</p><p>555555</p><p></p>66666</p>-------8 -----path:0<p>11111</p><p></p><p>22222</p><p>33333</p><p >44444</p><p>555555</p><p></p>66666</p>pos:68index1:-1index2:71pos:71 <p>11111</p><p></p><p>22222</p><p>33333</p><p>44444</ p><p>555555</p><p></p>66666</p>-------9-----path:1<p>11111</p> <p></p><p>22222</p><p>33333</p><p>44444</p><p>555555</p><p>< /p>66666</p>pos:71index1:-1index2:80pos:78<p>11111</p><p></p><p>22222 </p><p>33333</p><p>44444</p><p>555555</p><p></p><p >66666</p>------------path:0<p>11111</p><p></p><p>22222</p>< p>33333</p><p>44444</p><p>555555</p><p></p><p>66666</p >pos:78index1:-1index2:83pos:83<p>11111</p><p></p><p>22222</p><p> 33333</p><p>44444</p><p>555555</p><p></p><p>66666</p> <p>11111</p><p>22222</p><p>33333</p><p>44444</p><p> 555555</p><p>66666</p>
Analysis:
1. Since only the <P></P> is involved, <p> and </p> can be treated as two states, the mind map is shown above, and the string is processed from left to right.
2. Convert string to StringBuffer for easy insert operation.
3. Use the outer do while inner layer switch mode to traverse.
4. Use POS to record the current target location.
5. Using flag, when the termination condition is triggered in the Swtich, set it to false, then break, which terminates.
6. Trigger stop signal: A. The current POS is at the last </p>, this condition applies to two states; B. The current POS node is traversed backwards without </p>, proving that this is the last </p>
Note: Because the <p> status is not in the end state, it cannot be exited directly from Switch's case 0 and can only jump to case 1.
Java processing HTML code <p> tag guarantee closure