Get SubString Between Tags in Java | Code Factory

Donate : Link

Medium Blog : Link

Applications : Link

package com.codeFactory;


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


<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Code Factory</title>

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: Logo

You are commenting using your 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