Click here to Skip to main content
Click here to Skip to main content

Extracting substring from an HTML tag

, 7 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sanketbramhe
Software Developer (Junior)
India India
I am Computer Science graduate. A code enthusiast, however not best at any of the computer language. But of course have knowledge of all of them.

Comments and Discussions

 
QuestionRegex is better PinmemberHiren solanki7-Sep-12 21:02 
AnswerRe: Regex is better Pinmembersanketbramhe24-Sep-12 6:14 
QuestionWhy not regex Pinmemberfedekun7-Sep-12 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 7 Sep 2012
Article Copyright 2012 by sanketbramhe
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid