Skip to main content

Crate trim_newlines

Crate trim_newlines 

Source
Expand description

§trim-newlines — trim newlines from the start and end of a string

Remove leading and/or trailing newline characters (\r and \n only — not other whitespace) from a string. A faithful Rust port of the widely-used trim-newlines npm package.

use trim_newlines::{trim_newlines, trim_newlines_start, trim_newlines_end};

assert_eq!(trim_newlines("\n\nfoo\n\n"), "foo");
assert_eq!(trim_newlines_start("\r\nbar\r\n"), "bar\r\n");
assert_eq!(trim_newlines_end("\r\nbar\r\n"), "\r\nbar");

Only \r and \n are trimmed, so surrounding spaces or tabs are preserved. The functions return a borrowed slice of the input (no allocation).

Zero dependencies and #![no_std].

Functions§

trim_newlines
Trim newlines (\r and \n) from both ends of string.
trim_newlines_end
Trim newlines (\r and \n) from the end of string.
trim_newlines_start
Trim newlines (\r and \n) from the start of string.