Click here to Skip to main content

Extracting substring from an HTML tag


This code can be used in any programming language to extract a substring that is embedded inside an HTML tag.


There is Java Regex class with Pattern and Matcher that can be used to do this job. But I found it tedious and tricky. Hence I built my own version of code.

Using the code

Please be noted that this code can be used with any language. You do not have to use, import any class or package to use this code. Just take your HTML tag <OPEN TAG>some stuff</CLOSE TAG> into a string variable. In the following code, I have used the <Option> tag of HTML.  

public class TestString {
     * @param args
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String strReason = new String();
        String strText =  new String();
        String strValue = new String();
        String strSelect = new String();
        String strsubstrText = new String();
        strValue = "stringabc";
        strText = "stringpqr";
        strSelect = "stingxyz";
        strReason = "<option value=\"" +strValue+ "\"" + 
                    strSelect+ ">" +strText +"</option>" ;
        System.out .print(strReason);
        System.out .println();
        StringBuffer Modifed_strReason = new StringBuffer();
        for (int i= 0; i<strReason.length();i++)
            if(strReason.charAt(i)!='any character')
                continue;                  //continue untill you find desired character
            for (int j=i;j<strReason.length();j++)
                if(strReason.charAt(j)!='any character')
                 System.out .println (strReason.substring(i, j));     //final substring 
            i=j;        //set i at place of j
            break;      //to control looping of j

Web02 | 2.8.160208.1 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service