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

Extracting substring from an HTML tag

By , 7 Sep 2012
 

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)

About the Author

sanketbramhe
Software Developer (Junior) Mphasis Ltd
India India
Member
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.
Language like C#, Java, SQL\Pl-Sql are of my interest
I want to build my career inot Oracle developer with Forms and Reports.
Anyone having study material of this, please share with me.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRegex is better PinmemberHiren solanki7 Sep '12 - 20:02 
AnswerRe: Regex is better Pinmembersanketbramhe24 Sep '12 - 5:14 
QuestionWhy not regex Pinmemberfedekun7 Sep '12 - 10: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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 7 Sep 2012
Article Copyright 2012 by sanketbramhe
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid