Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Java
Tip/Trick

Extracting substring from an HTML tag

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Sep 2012CPOL 19.3K   2   3
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.  

Java
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)


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

Comments and Discussions

 
QuestionRegex is better Pin
Hiren solanki7-Sep-12 20:02
Hiren solanki7-Sep-12 20:02 
AnswerRe: Regex is better Pin
SanketAB24-Sep-12 5:14
SanketAB24-Sep-12 5:14 
QuestionWhy not regex Pin
fedekun7-Sep-12 10:56
fedekun7-Sep-12 10:56 
I just don't understand why not regex, if you want.

In Javascript:

var inside = "<tag>A random string</tag>".match(/\<(tag)\>(.*?)\<\/\1\>/i)[2];
var substring = ​inside.substring(0, 5);
console.log(substring); // Writes "A ran"

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.