如何在Java中删除文件中的换行符?

发布于 2024-08-19 23:31:11 字数 262 浏览 11 评论 0原文

如何以适用于 Windows 和 Linux 的方式替换 Java 中字符串中的所有换行符(即没有回车/换行/换行等操作系统特定问题)?

我已经尝试过(注意 readFileAsString 是一个将文本文件读入字符串的函数):

String text = readFileAsString("textfile.txt");
text.replace("\n", "");

但这似乎不起作用。

这怎么能做到呢?

How can I replace all line breaks from a string in Java in such a way that will work on Windows and Linux (ie no OS specific problems of carriage return/line feed/new line etc.)?

I've tried (note readFileAsString is a function that reads a text file into a String):

String text = readFileAsString("textfile.txt");
text.replace("\n", "");

but this doesn't seem to work.

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

故事未完 2024-08-26 23:31:11

您需要将 text 设置为 text.replace() 的结果:

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");

这是必要的,因为字符串是不可变的——调用 replace 并不可变更改原始字符串,它返回一个已更改的新字符串。如果您不将结果分配给 text,则该新字符串将丢失并被垃圾收集。

至于获取任何环境的换行符字符串——可以通过调用 System.getProperty("line.separator") 来实现。

You need to set text to the results of text.replace():

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");

This is necessary because Strings are immutable -- calling replace doesn't change the original String, it returns a new one that's been changed. If you don't assign the result to text, then that new String is lost and garbage collected.

As for getting the newline String for any environment -- that is available by calling System.getProperty("line.separator").

悍妇囚夫 2024-08-26 23:31:11

正如其他答案中所述,您的代码主要无法正常工作,因为 String.replace(...) 不会更改目标 String。 (它不能 - Java 字符串是不可变的!)replace 实际上所做的是创建并返回一个新的 String 对象,其中的字符根据需要进行更改。但是您的代码随后会丢弃该 String ...


以下是一些可能的解决方案。哪一种最正确取决于您到底想做什么。

// #1
text = text.replace("\n", "");

只需删除所有换行符即可。这不适用于 Windows 或 Mac 线路终端。

// #2
text = text.replace(System.getProperty("line.separator"), "");

删除当前平台的所有行终止符。这不适用于您尝试在 Windows 上处理(例如)UNIX 文件的情况,反之亦然。

// #3
text = text.replaceAll("\\r|\\n", "");

删除所有 Windows、UNIX 或 Mac 行终止符。但是,如果输入文件是文本,这将连接单词;例如

Goodbye cruel
world.

变成

Goodbye cruelworld.

所以你可能实际上想要这样做:

// #4
text = text.replaceAll("\\r\\n|\\r|\\n", " ");

用空格1替换每个行终止符。从 Java 8 开始,您还可以这样做:

// #5
text = text.replaceAll("\\R", " ");

如果您想用一个空格替换多行终止符:

// #6
text = text.replaceAll("\\R+", " ");

1 - 请注意,#3 和 #4 之间存在细微差别。序列 \r\n 表示单个 (Windows) 行终止符,因此我们需要小心不要将其替换为两个空格。

As noted in other answers, your code is not working primarily because String.replace(...) does not change the target String. (It can't - Java strings are immutable!) What replace actually does is to create and return a new String object with the characters changed as required. But your code then throws away that String ...


Here are some possible solutions. Which one is most correct depends on what exactly you are trying to do.

// #1
text = text.replace("\n", "");

Simply removes all the newline characters. This does not cope with Windows or Mac line terminations.

// #2
text = text.replace(System.getProperty("line.separator"), "");

Removes all line terminators for the current platform. This does not cope with the case where you are trying to process (for example) a UNIX file on Windows, or vice versa.

// #3
text = text.replaceAll("\\r|\\n", "");

Removes all Windows, UNIX or Mac line terminators. However, if the input file is text, this will concatenate words; e.g.

Goodbye cruel
world.

becomes

Goodbye cruelworld.

So you might actually want to do this:

// #4
text = text.replaceAll("\\r\\n|\\r|\\n", " ");

which replaces each line terminator with a space1. Since Java 8 you can also do this:

// #5
text = text.replaceAll("\\R", " ");

And if you want to replace multiple line terminator with one space:

// #6
text = text.replaceAll("\\R+", " ");

1 - Note there is a subtle difference between #3 and #4. The sequence \r\n represents a single (Windows) line terminator, so we need to be careful not to replace it with two spaces.

小瓶盖 2024-08-26 23:31:11

此函数将所有空白(包括换行符)规范化为单个空格。不完全是最初问题所要求的,但可能完全符合许多情况下所需要的:

import org.apache.commons.lang3.StringUtils;

final String cleansedString = StringUtils.normalizeSpace(rawString);

This function normalizes down all whitespace, including line breaks, to single spaces. Not exactly what the original question asked for, but likely to do exactly what is needed in many cases:

import org.apache.commons.lang3.StringUtils;

final String cleansedString = StringUtils.normalizeSpace(rawString);
独自唱情﹋歌 2024-08-26 23:31:11

如果您只想删除在当前操作系统上有效的行终止符,您可以这样做:

text = text.replaceAll(System.getProperty("line.separator"), "");

如果您想确保删除任何行分隔符,您可以这样做:

text = text.replaceAll("\\r|\\n", "");

或者,稍微更详细,但更少的正则表达式:

text = text.replaceAll("\\r", "").replaceAll("\\n", "");

If you want to remove only line terminators that are valid on the current OS, you could do this:

text = text.replaceAll(System.getProperty("line.separator"), "");

If you want to make sure you remove any line separators, you can do it like this:

text = text.replaceAll("\\r|\\n", "");

Or, slightly more verbose, but less regexy:

text = text.replaceAll("\\r", "").replaceAll("\\n", "");
何处潇湘 2024-08-26 23:31:11
str = str.replaceAll("\\r\\n|\\r|\\n", " ");

经过大量搜索后,对我来说效果非常好,其他所有行都失败了。

str = str.replaceAll("\\r\\n|\\r|\\n", " ");

Worked perfectly for me after searching a lot, having failed with every other line.

污味仙女 2024-08-26 23:31:11

我想这会很有效率

String s;
s = "try this\n try me.";
s.replaceAll("[\\r\\n]+", "")

This would be efficient I guess

String s;
s = "try this\n try me.";
s.replaceAll("[\\r\\n]+", "")
放赐 2024-08-26 23:31:11

windows/linux/mac 下换行符不一样。您应该使用 System.getProperties< /a> 带有属性 line.separator。

Linebreaks are not the same under windows/linux/mac. You should use System.getProperties with the attribute line.separator.

骑趴 2024-08-26 23:31:11

在 Kotlin 中,以及从 Java 11 开始,String 具有 lines() 方法,该方法返回多行字符串中的行列表。
您可以获取所有行,然后将它们合并成一个字符串。

使用 Kotlin 就变得很简单

str.lines().joinToString("")

In Kotlin, and also since Java 11, String has lines() method, which returns list of lines in the multi-line string.
You can get all the lines and then merge them into a single string.

With Kotlin it will be as simple as

str.lines().joinToString("")
长途伴 2024-08-26 23:31:11
String text = readFileAsString("textfile.txt").replaceAll("\n", "");

尽管oracle网站中trim()的定义是
“返回字符串的副本,省略前导和尾随空格。”

该文档省略了说明新行字符(前导和尾随)也将被删除。

简而言之
String text = readFileAsString("textfile.txt").trim(); 也适合您。
(用Java 6检查)

String text = readFileAsString("textfile.txt").replaceAll("\n", "");

Even though the definition of trim() in oracle website is
"Returns a copy of the string, with leading and trailing whitespace omitted."

the documentation omits to say that new line characters (leading and trailing) will also be removed.

In short
String text = readFileAsString("textfile.txt").trim(); will also work for you.
(Checked with Java 6)

℉絮湮 2024-08-26 23:31:11
String text = readFileAsString("textfile.txt").replace("\n","");

.replace 返回一个新字符串,Java 中的字符串是不可变的。

String text = readFileAsString("textfile.txt").replace("\n","");

.replace returns a new string, strings in Java are Immutable.

因为看清所以看轻 2024-08-26 23:31:11

您可能想使用 BufferedReader 读取文件。此类可以将输入分解为单独的行,您可以随意组合这些行。无论当前平台如何,BufferedReader 的操作方式都会自动识别 Linux、Windows 和 MacOS 世界的行结束约定。

因此:

BufferedReader br = new BufferedReader(
    new InputStreamReader("textfile.txt"));
StringBuilder sb = new StringBuilder();
for (;;) {
    String line = br.readLine();
    if (line == null)
        break;
    sb.append(line);
    sb.append(' ');   // SEE BELOW
}
String text = sb.toString();

请注意,readLine() 在返回的字符串中不包含行终止符。上面的代码附加了一个空格,以避免将一行的最后一个单词和下一行的第一个单词粘在一起。

You may want to read your file with a BufferedReader. This class can break input into individual lines, which you can assemble at will. The way BufferedReader operates recognizes line ending conventions of the Linux, Windows and MacOS worlds automatically, regardless of the current platform.

Hence:

BufferedReader br = new BufferedReader(
    new InputStreamReader("textfile.txt"));
StringBuilder sb = new StringBuilder();
for (;;) {
    String line = br.readLine();
    if (line == null)
        break;
    sb.append(line);
    sb.append(' ');   // SEE BELOW
}
String text = sb.toString();

Note that readLine() does not include the line terminator in the returned string. The code above appends a space to avoid gluing together the last word of a line and the first word of the next line.

自此以后,行同陌路 2024-08-26 23:31:11

我觉得很奇怪(Apache) StringUtils 这里还没有介绍。

您可以使用 .replace 方法从字符串中删除所有换行符(或任何其他出现的子字符串)。

StringUtils.replace(myString, "\n", "");

此行将用空字符串替换所有换行符。

因为换行符在技术上是一个字符,所以您可以选择使用 .replaceChars 方法来替换字符

StringUtils.replaceChars(myString, '\n', '');

I find it odd that (Apache) StringUtils wasn't covered here yet.

you can remove all newlines (or any other occurences of a substring for that matter) from a string using the .replace method

StringUtils.replace(myString, "\n", "");

This line will replace all newlines with the empty string.

because newline is technically a character you can optionally use the .replaceChars method that will replace characters

StringUtils.replaceChars(myString, '\n', '');
绳情 2024-08-26 23:31:11

仅供参考,如果您想用单行符替换同时多行符,那么您可以使用

myString.trim().replaceAll("[\n]{2,}", "\n")

或 替换为单个空格

myString.trim().replaceAll("[\n]{2,}", " ")

FYI if you can want to replace simultaneous muti-linebreaks with single line break then you can use

myString.trim().replaceAll("[\n]{2,}", "\n")

Or replace with a single space

myString.trim().replaceAll("[\n]{2,}", " ")
却一份温柔 2024-08-26 23:31:11

您可以使用 apache commons IOUtils 迭代该行并将每行附加到 StringBuilder。并且不要忘记关闭InputStream

StringBuilder sb = new StringBuilder();
FileInputStream fin=new FileInputStream("textfile.txt");
LineIterator lt=IOUtils.lineIterator(fin, "utf-8");
while(lt.hasNext())
{
  sb.append(lt.nextLine());
}
String text = sb.toString();
IOUtils.closeQuitely(fin);

You can use apache commons IOUtils to iterate through the line and append each line to StringBuilder. And don't forget to close the InputStream

StringBuilder sb = new StringBuilder();
FileInputStream fin=new FileInputStream("textfile.txt");
LineIterator lt=IOUtils.lineIterator(fin, "utf-8");
while(lt.hasNext())
{
  sb.append(lt.nextLine());
}
String text = sb.toString();
IOUtils.closeQuitely(fin);
半世晨晓 2024-08-26 23:31:11

您可以使用通用方法将任何字符替换为任何字符。

public static void removeWithAnyChar(String str, char replceChar,
        char replaceWith) {
    char chrs[] = str.toCharArray();
    int i = 0;
    while (i < chrs.length) {

        if (chrs[i] == replceChar) {
            chrs[i] = replaceWith;
        }
        i++;
    }

}

You can use generic methods to replace any char with any char.

public static void removeWithAnyChar(String str, char replceChar,
        char replaceWith) {
    char chrs[] = str.toCharArray();
    int i = 0;
    while (i < chrs.length) {

        if (chrs[i] == replceChar) {
            chrs[i] = replaceWith;
        }
        i++;
    }

}
浅听莫相离 2024-08-26 23:31:11

org.apache.commons.lang.StringUtils#chopNewline

org.apache.commons.lang.StringUtils#chopNewline

爱要勇敢去追 2024-08-26 23:31:11

尝试这样做:

 textValue= textValue.replaceAll("\n", "");
 textValue= textValue.replaceAll("\t", "");
 textValue= textValue.replaceAll("\\n", "");
 textValue= textValue.replaceAll("\\t", "");
 textValue= textValue.replaceAll("\r", "");
 textValue= textValue.replaceAll("\\r", "");
 textValue= textValue.replaceAll("\r\n", "");
 textValue= textValue.replaceAll("\\r\\n", "");

Try doing this:

 textValue= textValue.replaceAll("\n", "");
 textValue= textValue.replaceAll("\t", "");
 textValue= textValue.replaceAll("\\n", "");
 textValue= textValue.replaceAll("\\t", "");
 textValue= textValue.replaceAll("\r", "");
 textValue= textValue.replaceAll("\\r", "");
 textValue= textValue.replaceAll("\r\n", "");
 textValue= textValue.replaceAll("\\r\\n", "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文