veraliesim
veraliesim

Reputation: 31

Split a string in java containing html

I have a dynamically generated string like :

String s = <span><input style='font-weight:bold'>Hello team</input></span>

I want to split the string as:

String startTag = <span><input style='font-weight:bold'>
String endTag = </input></span>
String content = Hello Team

The String s can be anything (depending on the code) like

<span style='font-weight:bold'>Hello team</span>

or

<td><input style='font-weight:bold'>Hello team</input></td>

So, I want to split based on the index of '>' and '<'?

How can I achieve that?

Upvotes: 0

Views: 2117

Answers (5)

Bohemian
Bohemian

Reputation: 425208

Use this (single line) to split:

String[] parts = s.split("(?<=>)(?=((?!<[^/]).)*$)|(?=</)", 3);

This splits the input into an array of size 3:

parts[0] // opening tag(s)
parts[1] // content
parts[2] // closing tag(s)

It works for any number of wrapping tags.

Upvotes: 0

azro
azro

Reputation: 54168

public static void main(String[] args) {
  String s = "<td><span><td><input style='font-weight:bold'>Hello team</input></td></span></td>";

  Pattern p = Pattern.compile("^(<.+>)([a-z A-Z ]+?)(</.+>)$");

  Matcher m = p.matcher(s);

  if(m.matches()) {
        System.out.println(m.group(1));
        System.out.println(m.group(2));
        System.out.println(m.group(3)); 
  }
}

This, is gonna work, m.group(0) is the hole string so don't use it It uses regex : regular expression to catch normalized sentences , you find lots of example on the web, lots of programation languages have their proper rules for regex be careful

Upvotes: 0

Julien Gauthier
Julien Gauthier

Reputation: 114

You can also try to use a SAX Parser. Implement your own DefaultHandler and override the following methods :

public void characters(char[] ch, int start, int length)

public void startElement (String uri, String localName, String qName, Attributes attributes)

public void endElement (String uri, String localName, String qName)

If you need help, look this example : https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

Good luck

Upvotes: 1

veraliesim
veraliesim

Reputation: 31

I used the below and it works fine for me. Thanks for the help! :)

               int i2 = s.indexOf(">");
            int count = 0;
            LinkedList<Integer> indexes = new LinkedList<Integer>();
            while (i2 >= 0) {
               indexes.add(i2);
               i2 = s.indexOf(">", i2 + 1);
               count ++;
           }
            int i1 = s.indexOf("</");
            int c = count/2;
            int b = indexes.get(c-1);

            String startTag = s.substring(0,b+1);
            String content = s.substring(b+1,i1);
            String endTag = s.substring(i1);

Upvotes: 0

Claudiu Haidu
Claudiu Haidu

Reputation: 827

public class Program{
public static void main(String[] args) {
    String s = "<span><input style='font-weight:bold'>Hello team</input></span>";
    String sCheck = s;

    int j=0;
    int k=0;
    String startTag="";
    String storedStartTag="";
    String endTag;
    String storedEndTag="";
    boolean foundEnd=false;

    if(s.charAt(0) == '<'){

        for (int i = 0;i<sCheck.length();i++){
            if(sCheck.charAt(i) == '>'){
                j=i;
                startTag = sCheck.substring(0,j+1);
                storedStartTag = storedStartTag + startTag;
                sCheck = sCheck.substring(j+1,sCheck.length());
            }
        }
    }

   for (int i = 0;i<s.length();i++){
            if(s.charAt(i) == '<'){
                if(s.charAt(i+1) == '/'){
                    k=i;
                    foundEnd = true;
                } 
            }
           if (foundEnd == true){
                    if(s.charAt(i) == '>'){
                       endTag = s.substring(k,i+1);
                       storedEndTag = storedEndTag + endTag;
                    }
            }

    }
    System.out.println(storedStartTag);
    System.out.println(storedEndTag);
}}

This is without Regex, just tried to solve it, don't judge :))

Upvotes: 0

Related Questions