How to remove newlines from beginning and end of a string?

前端 未结 12 597
旧巷少年郎
旧巷少年郎 2020-11-29 03:22

I have a string that contains some text followed by a blank line. What\'s the best way to keep the part with text, but remove the whitespace newline from the end?

相关标签:
12条回答
  • 2020-11-29 03:31

    This Java code does exactly what is asked in the title of the question, that is "remove newlines from beginning and end of a string-java":

    String.replaceAll("^[\n\r]", "").replaceAll("[\n\r]$", "")
    

    Remove newlines only from the end of the line:

    String.replaceAll("[\n\r]$", "")
    

    Remove newlines only from the beginning of the line:

    String.replaceAll("^[\n\r]", "")
    
    0 讨论(0)
  • 2020-11-29 03:33
    String.replaceAll("[\n\r]", "");
    
    0 讨论(0)
  • 2020-11-29 03:34

    tl;dr

    String cleanString = dirtyString.strip() ; // Call new `String::string` method.
    

    String::strip…

    The old String::trim method has a strange definition of whitespace.

    As discussed here, Java 11 adds new strip… methods to the String class. These use a more Unicode-savvy definition of whitespace. See the rules of this definition in the class JavaDoc for Character::isWhitespace.

    Example code.

    String input = " some Thing ";
    System.out.println("before->>"+input+"<<-");
    input = input.strip();
    System.out.println("after->>"+input+"<<-");
    

    Or you can strip just the leading or just the trailing whitespace.

    You do not mention exactly what code point(s) make up your newlines. I imagine your newline is likely included in this list of code points targeted by strip:

    • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
    • It is '\t', U+0009 HORIZONTAL TABULATION.
    • It is '\n', U+000A LINE FEED.
    • It is '\u000B', U+000B VERTICAL TABULATION.
    • It is '\f', U+000C FORM FEED.
    • It is '\r', U+000D CARRIAGE RETURN.
    • It is '\u001C', U+001C FILE SEPARATOR.
    • It is '\u001D', U+001D GROUP SEPARATOR.
    • It is '\u001E', U+001E RECORD SEPARATOR.
    • It is '\u001F', U+0
    0 讨论(0)
  • 2020-11-29 03:35

    Another elegant solution.

    String myString = "\nLogbasex\n";
    myString = org.apache.commons.lang3.StringUtils.strip(myString, "\n");
    
    0 讨论(0)
  • For anyone else looking for answer to the question when dealing with different linebreaks:

    string.replaceAll("(\n|\r|\r\n)$", ""); // Java 7
    string.replaceAll("\\R$", "");          // Java 8
    

    This should remove exactly the last line break and preserve all other whitespace from string and work with Unix (\n), Windows (\r\n) and old Mac (\r) line breaks: https://stackoverflow.com/a/20056634, https://stackoverflow.com/a/49791415. "\\R" is matcher introduced in Java 8 in Pattern class: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

    This passes these tests:

    // Windows:
    value = "\r\n test \r\n value \r\n";
    assertEquals("\r\n test \r\n value ", value.replaceAll("\\R$", ""));
    
    // Unix:
    value = "\n test \n value \n";
    assertEquals("\n test \n value ", value.replaceAll("\\R$", ""));
    
    // Old Mac:
    value = "\r test \r value \r";
    assertEquals("\r test \r value ", value.replaceAll("\\R$", ""));
    
    0 讨论(0)
  • 2020-11-29 03:38

    Try this

    function replaceNewLine(str) { 
      return str.replace(/[\n\r]/g, "");
    }
    
    0 讨论(0)
提交回复
热议问题