如何在msword列表中的每个单词后面添加引号、逗号和空格?

2024-04-24 08:46:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在word文档中的每个单词后面添加引号和逗号。你知道吗

For example:

apple
banana
carrot

Goes into:

"apple",
"banana",
"carrot",

我应该在find部分和replace部分中输入什么来获得这个结果?你知道吗


Tags: 文档appleforexamplefind单词replace引号
2条回答

不知道为什么你的问题中同时有JavaScript和Java标记,但我假设你指的是Java。一种可能的解决方案是使用Java执行以下操作:

1)让我们先读入包含单词的文件,然后创建一种写出结果的方法:

String contents = new String(Files.readAllBytes(Paths.get("path-to-input"))); // input
BufferedWriter writer = new BufferedWriter(new FileWriter("path-to-output")); // output

2)我们已经将文件的内容读取为单个字符串,现在将其拆分为单个单词。我们将通过按回车符和换行符进行拆分(根据您的文件,只有换行符可能足够):

String[] splitContents = contents.split("\r\n");

3)让我们将上面数组中的字符串更改为希望在文件中看到的字符串。然后输出到文件:

    for(int i = 0; i < splitContents.length; i++) {
        splitContents[i] = "\"" + splitContents[i] + "\","; //create new string
        writer.write(splitContents[i]); //write new string to file
        writer.newLine(); //append newline
    }

4)别忘了flush()close()writer

writer.flush();
writer.close();

LibreOffice Writer上,您可以按CTRL + H打开查找和替换工具。你知道吗

必须启用正则表达式并进行如下配置:

查找:(\w+)

替换:"$1"

然后按“全部替换”。你知道吗

我相信你也能在word上做到。你知道吗

LibreOffice Writer

相关问题 更多 >