java - Java中是否有任何内置函数可以从提取的数据中删除不需要的数据
问题描述
我从文本文件中提取了一些文本,但现在我只想要该文本中的一些特定单词。
我尝试过的是从该文本文件中读取的,并且我使用关键字进行了搜索:
FileReader fr = new
FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
BufferedReader br = new BufferedReader(fr);
String s;
String keyword = "dba COPIEFacture ";
while ((s = br.readLine()) != null) {
if (s.contains(keyword)) {
System.out.println(s);
我得到这样的输出:dba COPIEFacture du 28/05/2018 n° 10077586115Récapitulatif de vote facture
但我只想要 28/05/2018 这个所以请帮助我
解决方案
这会成功的。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
FileReader fr;
String keyword = "dba COPIEFacture du ";
String textToFind = "28/05/2018"; // The length usually will not
// change.You can use value
// 10(length) instead
StringBuilder sb = new StringBuilder();
try {
fr = new FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
int i;
while ((i = fr.read()) != -1) {
sb.append((char) i);
}
int start = sb.indexOf(keyword) + keyword.length();
int end = start + textToFind.length();
System.out.print(sb.substring(start, end)); //output: 28/05/2018
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐阅读
- react-native - 从组件导航到屏幕反应原生
- laravel - Laravel - 验证正确的格式
- python - 如何在 virtualenv 中安装 Python 3.6.x?
- python - 如何手动创建熊猫数据框
- node.js - express.js 中嵌套异步函数中的错误处理
- jquery - 按热键跳过 li 不工作 jquery
- go - Golang 加密消息修复 go-crypt()
- c++ - 在 Visual Studio 中编译我的 Visual C++ 文件后,会创建一个 3D 对象文件而不是对象文件
- r - 当我在 R 中使用 Pipe 时,这个错误是什么
- php - codeigniter中数据库中2个表的下拉列表