Java如何在两个字符串之间查找文本?
在此示例中,我们将使用StringUtils.substringBetween()方法。在这里,我们将提取HTML文档的标题和正文。让我们看一下代码。
package org.nhooo.example.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);
}
}通过打印标题和内容,我们将看到类似以下内容的内容:
title = Hello World from Java content = Hello, today is: Wed Jul 24 10:34:49 WITA 2019
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>热门推荐
10 过年给美女祝福语简短
11 恭喜祝福语回复语句简短
12 娃娃感冒生日祝福语简短
13 妹妹生日宴祝福语简短
14 企业开工花束祝福语简短
15 孩子过生日简短祝福语
16 学生小升初祝福语简短励志
17 横幅生日老板祝福语简短
18 顺利毕业家人祝福语简短