gpt4 book ai didi

java - 如何从文本文件的一行中删除两个字符之间的空格

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:08 26 4
gpt4 key购买 nike

我有一个像下面这样的字符串作为文本文件中的一行

Fri Oct 23 09:59:04.975 CDT 2015 - default- WHITESPACE -

2015 年 10 月 23 日星期五 09:59:04.975 CDT - 默认 - -187891074:

我想删除 default--随机数 之间的空格。看第二行。 default- 位于固定位置,但第二个- 在文件中没有固定位置。

这是我一直在尝试的

FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("output.txt");
String line

while((line = br.readLine()) != null)
{
if(line.contains("default-")) {

}
}

预期输出

Fri Oct 23 09:59:04.975 CDT 2015 - default--187891074:

最佳答案

我会使用 String.replaceFirst()

line = line.replaceFirst("(default-) +(-|\\d)", "$1$2");

关于java - 如何从文本文件的一行中删除两个字符之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33310673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com