Advertisement
ksoltan

JM Chapter 10 #14

Mar 11th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. Write a method that checks whether a string has matching HTML tags. If they match, return the string without the tags. Otherwise, return the string as is.
  2.  
  3. JM Chapter 10 #14
  4.  
  5. "<Hello>WORLD</Hello>", "WORLD"
  6. "<b>I_Like_Pie/<Cake></b>", "I_Like_Pie/<Cake>"
  7. "<b>HelloThere</c>", "<b>HelloThere</c>"
  8. "", ""
  9. "Hi", "Hi"
  10. "Ender'sGame<b></b>", "Ender'sGame<b></b>"
  11. "<b></b>", ""
  12. "<b></b>HI", "<b></b>HI"
  13. "CircleOfLife/", "CircleOfLife/"
  14. "<kop>POP</kop>", "POP"
  15. "</kop>TOP<kop>", "</kop>TOP<kop>"
  16. "<bookthief>1984</bookthief>", "1984"
  17. "<J><<<<<</J>", "<<<<<"
  18. "<J><<<<<<J>", "<J><<<<<<J>"
  19. "YOUDIDIT!!", "YOUDIDIT!!"
  20.  
  21. public String removeTag(String str) {
  22. if (str.length() < 4) {
  23. return str;
  24. }
  25. if (str.indexOf('<') == 0 && str.lastIndexOf('>') == str.length() - 1) {
  26. int i = str.indexOf('>');
  27. int j = str.lastIndexOf('<');
  28. if (("</" + str.substring(1, i + 1)).equals(str.substring(j))) {
  29. return str.substring(i+1, j);
  30. }
  31. }
  32. return str;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement