我正在尝试删除时间表中的alle 30 Min
字样。目前正在删除30
和Min
,但不会删除alle
字样。我该如何解决这个问题?
我感谢任何帮助。
示例
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
没有延迟的最终输出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
代码:
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);
}
答案 0 :(得分:2)
将结果字符串分配回行!
line = line.replace(" alle ", " ");
工作示例:
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();
}
}
}
<强>输出:强>
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
答案 1 :(得分:1)
你在单词alle之前和之后都有空格,所以删除它。
line.replace("alle", " ");
答案 2 :(得分:1)
replace(...)
方法无法修改原始String
,它会返回新修改的String
。你需要做这样的事情:
line = line.replace(" alle ", " ");
这是一个完整的解决方案:
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);
}