【问题标题】:Delete word from a line in a text file从文本文件的一行中删除单词
【发布时间】:2015-06-06 07:47:36
【问题描述】:

我正在尝试删除时间表行中的 alle 30 Min 字样。目前30Min 正在被删除,但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

exepted output without alle 30 Min:

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);
    }

【问题讨论】:

  • 你能告诉我们你得到什么输出和你期望什么?比如,一个样本。

标签: java string file replace


【解决方案1】:

结果字符串分配回行!

line = line.replace(" alle ", " ");

参考String#replace


工作示例:

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

【讨论】:

  • 不抱歉,我正在寻找alle
  • @TheBook 应该把chairalle 变成chair 吗?假设这是您想要的,请删除 alle 周围的空格
  • @Gosu 也许是德国数据源。如果您查看他的样本数据,您会发现一个“alle”。
  • @Gosu:什么? chairalle这个词中的子串alle不应该被删除我只想删除这种格式的alle space+alle+space
  • @DirkLachowski 并且考虑到他当前的代码,alle 将被删除,我认为他想从 chairalletablealle 中删除 alle..
【解决方案2】:

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);
}

【讨论】:

  • 它现在可以工作了,谢谢,但为什么 30Min 都戴上了它?
  • 这些值在您存储在nextLinenextLine2 中的行上,您永远不会将这些行添加到writer
  • @TheBook 您的代码中还有其他错误,我已经编辑了答案以包含完整的解决方案。
【解决方案3】:

你在 alle 之前和之后都有空格,所以删除它。

line.replace("alle", " ");

【讨论】:

  • 我在没有空格的情况下尝试了它,除了椅子上的子字符串 alle 之外它仍然不起作用,其他单词不应该被删除因为我在 if 语句中使用空格
  • @TheBook 您没有将替换的结果字符串分配给您的 String 变量,请检查我更新的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2013-05-13
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多