In this example we’ll use the StringUtils.substringBetween()
method. Here we’ll extract the title and body of our HTML document. Let’s see the code.
package org.kodejava.commons.lang;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
public class NestedString {
public static void main(String[] args) {
String helloHtml = "<html>" +
"<head>" +
" <title>Hello World from Java</title>" +
"<body>" +
"Hello, today is: " + new Date() +
"</body>" +
"</html>";
String title = StringUtils.substringBetween(helloHtml, "<title>", "</title>");
String content = StringUtils.substringBetween(helloHtml, "<body>", "</body>");
System.out.println("title = " + title);
System.out.println("content = " + content);
}
}
By printing out the title and content, we’ll see something similar to:
title = Hello World from Java
content = Hello, today is: Thu Sep 30 06:32:32 CST 2021
Maven Dependencies
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Hi, How to get text between a tag like
<a href rel="nofollow">some text here</a>
Wont work because
<a>
has some attributes inside.Hi Richa,
You can try using regex as the following example: