Get SubString Between Tags in Java | Code Factory

Reference Link : Link

Donate : 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.