65.9K
CodeProject is changing. Read more.
Home

Extracting substring from an HTML tag

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Sep 7, 2012

CPOL
viewsIcon

19893

This code extracts a substring embedded in a variable in an HTML tag.

Introduction

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

Background

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')
            {
                Modifed_strReason.append(strReason.charAt(i));
                continue;                  //continue untill you find desired character
            }
            Modifed_strReason.append(strReason.charAt(i));    
            i++;
            
            for (int j=i;j<strReason.length();j++)
            {
                if(strReason.charAt(j)!='any character')
                {
                   continue;
                }
                 System.out .println (strReason.substring(i, j));     //final substring 
            }
            i=j;        //set i at place of j
            break;      //to control looping of j
        }
    }