Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- JM Chapter 10 #14
- "<Hello>WORLD</Hello>", "WORLD"
- "<b>I_Like_Pie/<Cake></b>", "I_Like_Pie/<Cake>"
- "<b>HelloThere</c>", "<b>HelloThere</c>"
- "", ""
- "Hi", "Hi"
- "Ender'sGame<b></b>", "Ender'sGame<b></b>"
- "<b></b>", ""
- "<b></b>HI", "<b></b>HI"
- "CircleOfLife/", "CircleOfLife/"
- "<kop>POP</kop>", "POP"
- "</kop>TOP<kop>", "</kop>TOP<kop>"
- "<bookthief>1984</bookthief>", "1984"
- "<J><<<<<</J>", "<<<<<"
- "<J><<<<<<J>", "<J><<<<<<J>"
- "YOUDIDIT!!", "YOUDIDIT!!"
- public String removeTag(String str) {
- if (str.length() < 4) {
- return str;
- }
- if (str.indexOf('<') == 0 && str.lastIndexOf('>') == str.length() - 1) {
- int i = str.indexOf('>');
- int j = str.lastIndexOf('<');
- if (("</" + str.substring(1, i + 1)).equals(str.substring(j))) {
- return str.substring(i+1, j);
- }
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement