Get SubString Between Tags in Java | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

SubstringBetween.java

package com.codeFactory;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author code.factory
 *
 */
public class SubstringBetween {

	public static void main(String... args) throws IOException {
		File file = new File("TestHTML.html");
		String htmlStr = FileUtils.readFileToString(file);

		String title = StringUtils.substringBetween(htmlStr, "<title>", "</title>");
		System.out.println("Title : " + title);

		String[] tds = StringUtils.substringsBetween(htmlStr, "<td>", "</td>");
		for (String td : tds) {
			System.out.println("Td : " + td);
		}
	}
}

TestHTML.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Code Factory</title>
</head>
<body>
<table>
	<tr>
		<td>One</td>
		<td>Two</td>
	</tr>
	<tr>
		<td>Three</td>
		<td>Four</td>
	</tr>
</table>
</body>
</html>

Output :

Title : Code Factory
Td : One
Td : Two
Td : Three
Td : Four

Note : i used commons-io-1.3.2.jar and commons-lang3-3.1.jar for FileUtils and StringUtils.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s