mr_nobody
mr_nobody

Reputation: 194

split string by tag java

I have string

stringst...ringstring11<jscript>qweqweqw....eqwe</jscript>22string..string

I have to remove everything between the tags

stringst...ringstring1122string..string

please tell me how to do it

String[] split = response.split("\\<jscript\\>");

it does not work

Upvotes: 0

Views: 1896

Answers (3)

Federico Piazza
Federico Piazza

Reputation: 31045

You can also use the old friend apache common. It has StringUtils.removePattern.

Documentation says

public static String removePattern(String source, String regex)

Removes each substring of the source String that matches the given regular expression using the DOTALL option

You can use it in the following way:

String result = StringUtils.removePattern(response, "<jscript>[^<]*?</jscript>");

It is worth mentioning that parsing html with regex could put you inside a black hole and will decrement your Life expectancy. Updated: added Robby Cornelissen suggestion

Upvotes: 1

Austin
Austin

Reputation: 3080

If you're parsing something like HTML and it is a single <jscript></jscript> You could do something like

int frontIndex = originalString.IndexOf('<jscript>');
int backIndex =  originalString.IndexOf('</jscript>');

String front = originalString.substring(0,frontIndex); 
String back = originalString.substring(backIndex,originalString.length()); 

originalString = front + back;

This will take everything before and up to <jscript>, everything after </jscript> and until the string.length(), then concatenate them together and replace the original string.

Upvotes: 2

Adam Yost
Adam Yost

Reputation: 3625

Use a regex and replaceAll to replace the tags and anything in between them with nothing.

Quick and dirty:

str = str.replaceAll("<jscript>[^<]*?</jscript>","");

It should be noted this is not a scalable solution and has serious limitations. HTML should not be parsed with regex as a rule, but this is a simple way which works within those limitations.

Limitations:

  • Fails if text between tags contains "<"
  • Fails if tags are not jscript

Upvotes: 2

Related Questions