简体   繁体   English

从 Java 字符串中删除行尾字符

[英]Remove end of line characters from Java string

I have string like this我有这样的字符串

"hello
java
book"

I want remove \\r and \\n from String(hello\\r\\njava\\r\\nbook) .我想从String(hello\\r\\njava\\r\\nbook)删除\\r\\n I want the result to be "hellojavabook" .我希望结果是"hellojavabook" How can I do this?我怎样才能做到这一点?

Regex with replaceAll.带有 replaceAll 的正则表达式。

public class Main
{
    public static void main(final String[] argv) 
    {
        String str;

        str = "hello\r\njava\r\nbook";
        str = str.replaceAll("(\\r|\\n)", "");
        System.out.println(str);
    }
}

If you only want to remove \\r\\n when they are pairs (the above code removes either \\r or \\n) do this instead:如果您只想在成对时删除 \\r\\n(上面的代码删除了 \\r 或 \\n),请执行以下操作:

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

If you want to avoid the regex, or must target an earlier JVM, String.replace() will do:如果您想避免使用正则表达式,或者必须针对较早的 JVM,则 String.replace() 将执行以下操作:

str=str.replace("\r","").replace("\n","");

And to remove a CRLF pair:并删除一个 CRLF 对:

str=str.replace("\r\n","");

The latter is more efficient than building a regex to do the same thing.后者比构建正则表达式来做同样的事情更有效。 But I think the former will be faster as a regex since the string is only parsed once.但我认为前者作为正则表达式会更快,因为字符串只被解析一次。

public static void main(final String[] argv) 
{
    String str;

    str = "hello\r\n\tjava\r\nbook";
    str = str.replaceAll("(\\r|\\n|\\t)", "");
    System.out.println(str);
}

It would be useful to add the tabulation in regex too.在正则表达式中添加表格也会很有用。

Given a String str:给定一个字符串 str:

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

You can either directly pass line terminator eg \\n, if you know the line terminator in Windows, Mac or UNIX.如果您知道 Windows、Mac 或 UNIX 中的行终止符,您可以直接传递行终止符,例如 \\n。 Alternatively you can use following code to replace line breaks in either of three major operating system.或者,您可以使用以下代码替换三个主要操作系统中的任何一个中的换行符。

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

Above code line will replace line breaks with space in Mac, Windows and Linux.上面的代码行将在 Mac、Windows 和 Linux 中用空格替换换行符。 Also you can use line-separator.您也可以使用行分隔符。 It will work for all OS.它适用于所有操作系统。 Below is the code snippet for line-separator.下面是行分隔符的代码片段。

String lineSeparator=System.lineSeparator();
String newString=yourString.replace(lineSeparator, "");
static byte[] discardWhitespace(byte[] data) {
    byte groomedData[] = new byte[data.length];
    int bytesCopied = 0;

    for (int i = 0; i < data.length; i++) {
        switch (data[i]) {
            case (byte) '\n' :
            case (byte) '\r' :
                break;
            default:
                groomedData[bytesCopied++] = data[i];
        }
    }

    byte packedData[] = new byte[bytesCopied];

    System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);

    return packedData;
}

Code found on commons-codec project.在 commons-codec 项目中找到的代码。

You can use unescapeJava from org.apache.commons.text.StringEscapeUtils like below您可以从org.apache.commons.text.StringEscapeUtils使用unescapeJava ,如下所示

str = "hello\r\njava\r\nbook";
StringEscapeUtils.unescapeJava(str);

您是否尝试过使用replaceAll方法将任何出现的 \\n 或 \\r 替换为空字符串?

嘿,我们也可以使用这个正则表达式解决方案。

String chomp = StringUtils.normalizeSpace(sentence.replaceAll("[\\r\\n]"," "));

Try below code.试试下面的代码。 It worked for me.它对我有用。

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

Did you try你试过了吗

string.trim(); 

This is meant to trim all leading and leaning while spaces in the string.这意味着在字符串中的空格时修剪所有前导和倾斜。 Hope this helps.希望这可以帮助。

Edit: (I was not explicit enough)编辑:(我不够明确)

So, when you string.split(), you will have a string[] - for each of the strings in the array, do a string.trim() and then append it.因此,当您使用 string.split() 时,您将有一个 string[] - 对于数组中的每个字符串,执行一个 string.trim() 然后附加它。

String[] tokens = yourString.split(" ");
StringBuffer buff = new StringBuffer();
for (String token : tokens)
{
  buff.append(token.trim());
}

Use stringBuffer/Builder instead of appending in the same string.使用 stringBuffer/Builder 而不是附加在同一个字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM