[英]Delete word from a line in a text file
I am trying to delete the words alle 30 Min
in the lines of my timetable. 我正尝试在时间表中删除
alle 30 Min
一词。 Currently 30
and Min
is being deleted but not the word alle
. 目前
30
和Min
被删除,但是alle
这个词还没有。 How can I fix that? 我该如何解决?
I appreciate any help. 感谢您的帮助。
Sample 样品
apple 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
chairalle 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
tablealle 05:09 05:39 06:11 06:41 07:11 07:41 08:11 alle 17:11 17:41 18:11 18:41 19:11 19:41
spoonalle 05:11 05:41 06:14 06:44 07:14 30 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
cabelalle 05:13 05:43 06:17 06:47 07:17 07:47 08:17 17:17 17:47 Min 18:17 18:47 19:17 19:47
exepted output without alle 30 Min: 无极限的出色输出30分钟:
apple 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
chairalle 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
tablealle 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
spoonalle 05:11 05:41 06:14 06:44 07:14 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
cabelalle 05:13 05:43 06:17 06:47 07:17 07:47 08:17 17:17 17:47 18:17 18:47 19:17 19:47
Code: 码:
Scanner scannerLines = new Scanner(file)) {
while (scannerLines.hasNextLine()) {
String line = scannerLines.nextLine();
if (line.contains(" alle ")) {
//Delete alle from the line.
line.replace(" alle ", " ");
String nextLine = scannerLines.nextLine();
Pattern pattern = Pattern.compile("\\s\\d\\d\\s");
Matcher m = pattern.matcher(nextLine);
while (m.find()) {
value = Integer.parseInt(m.group().trim());
line.replace(m.group(), " ");
String nextLine2 = scannerLines.nextLine();
nextLine2.replace("Min", " ");
System.out.println(value);
}
}
writer.println(line);
}
Assign the resulting string back to line! 将结果字符串分配回行!
line = line.replace(" alle ", " ");
Refer to String#replace 请参考String#replace
Working example: 工作示例:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class TestReplace {
public static void main (String [] args) {
File inputFile = new File("Input.txt");
File outputFile = new File("Output.txt");
Scanner sc = null;
FileWriter fw = null;
try {
sc = new Scanner(inputFile);
fw = new FileWriter(outputFile);
while(sc.hasNextLine()) {
String str = sc.nextLine();
str = str.replaceAll("\\s30\\s|\\salle\\s|\\sMin\\s", " ");
fw.write(str + "\r\n");
}
sc.close();
fw.close();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output: 输出:
apple 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
chairalle 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
tablealle 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
spoonalle 05:11 05:41 06:14 06:44 07:14 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
cabelalle 05:13 05:43 06:17 06:47 07:17 07:47 08:17 17:17 17:47 18:17 18:47 19:17 19:47
您在alle单词前后都有空格,因此请将其删除。
line.replace("alle", " ");
The replace(...)
method doesn't modify the original String
, it returns a new modified String
. replace(...)
方法不会修改原始的String
,而是返回一个新的修改后的String
。 You need to do something like this: 您需要执行以下操作:
line = line.replace(" alle ", " ");
And here is a complete solution: 这里是一个完整的解决方案:
Scanner scannerLines = new Scanner(file);
boolean alleFound = false;
boolean nrFound = false;
boolean minFound = false;
while (scannerLines.hasNextLine()) {
String line = scannerLines.nextLine();
if (!alleFound && line.contains(" alle ")) {
line = line.replace(" alle ", " ");
alleFound = true;
} else if (alleFound && !nrFound) {
line = line.replaceFirst("\\s\\d\\d\\s", " ");
nrFound = true;
} else if (alleFound && nrFound && !minFound) {
line = line.replace("Min", " ");
minFound = true;
}
writer.println(line);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.