Click here to Skip to main content
13,595,908 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 7 Sep 2012
Licenced CPOL

Extracting substring from an HTML tag

, 7 Sep 2012
Rate this:
Please Sign up or sign in to vote.
This code extracts a substring embedded in a variable in 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


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


About the Author

Software Developer (Senior)
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

QuestionRegex is better Pin
Hiren solanki7-Sep-12 20:02
memberHiren solanki7-Sep-12 20:02 
AnswerRe: Regex is better Pin
sanketbramhe24-Sep-12 5:14
membersanketbramhe24-Sep-12 5:14 
QuestionWhy not regex Pin
fedekun7-Sep-12 10:56
memberfedekun7-Sep-12 10:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 7 Sep 2012
Article Copyright 2012 by SanketAB
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid