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

前端 未结 12 598
旧巷少年郎
旧巷少年郎 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:41

    If you only want to remove line breaks (not spaces, tabs) at the beginning and end of a String (not inbetween), then you can use this approach:

    Use a regular expressions to remove carriage returns (\\r) and line feeds (\\n) from the beginning (^) and ending ($) of a string:

     s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", "")
    

    Complete Example:

    public class RemoveLineBreaks {
        public static void main(String[] args) {
            var s = "\nHello\nWorld\n";
            System.out.println("before: >"+s+"<");
            s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", "");
            System.out.println("after: >"+s+"<");
        }
    }
    

    It outputs:

    before: >
    Hello
    World
    <
    after: >Hello
    World<
    
    0 讨论(0)
  • 2020-11-29 03:42

    Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

    String trimmedString = myString.trim();
    
    0 讨论(0)
  • 2020-11-29 03:51

    If your string is potentially null, consider using StringUtils.trim() - the null-safe version of String.trim().

    0 讨论(0)
  • 2020-11-29 03:51

    I'm going to add an answer to this as well because, while I had the same question, the provided answer did not suffice. Given some thought, I realized that this can be done very easily with a regular expression.

    To remove newlines from the beginning:

    // Trim left
    String[] a = "\n\nfrom the beginning\n\n".split("^\\n+", 2);
    
    System.out.println("-" + (a.length > 1 ? a[1] : a[0]) + "-");
    

    and end of a string:

    // Trim right
    String z = "\n\nfrom the end\n\n";
    
    System.out.println("-" + z.split("\\n+$", 2)[0] + "-");
    

    I'm certain that this is not the most performance efficient way of trimming a string. But it does appear to be the cleanest and simplest way to inline such an operation.

    Note that the same method can be done to trim any variation and combination of characters from either end as it's a simple regex.

    0 讨论(0)
  • 2020-11-29 03:55
    String trimStartEnd = "\n TestString1 linebreak1\nlinebreak2\nlinebreak3\n TestString2 \n";
    System.out.println("Original String : [" + trimStartEnd + "]");
    System.out.println("-----------------------------");
    System.out.println("Result String : [" + trimStartEnd.replaceAll("^(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])|(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])$", "") + "]");
    
    1. Start of a string = ^ ,
    2. End of a string = $ ,
    3. regex combination = | ,
    4. Linebreak = \r\n|[\n\x0B\x0C\r\u0085\u2028\u2029]
    0 讨论(0)
  • 2020-11-29 03:56
    String text = readFileAsString("textfile.txt");
    text = text.replace("\n", "").replace("\r", "");
    
    0 讨论(0)
提交回复
热议问题