pub trait StringExtensions {
    // Required methods
    fn trim_start_bytes(&mut self, amount: usize);
    fn trim_end_bytes(&mut self, amount: usize);
    fn trim_in_place(&mut self);
    fn trim_start_in_place(&mut self);
    fn trim_end_in_place(&mut self);
    fn trim_matches_in_place<P>(&mut self, pat: P)
       where for<'a> P: UsableDoubleEndedPattern<'a>;
    fn trim_start_matches_in_place<P>(&mut self, pat: P)
       where for<'a> P: Pattern<'a>;
    fn strip_prefix_in_place<P>(&mut self, prefix: P) -> bool
       where for<'a> P: Pattern<'a>;
    fn strip_suffix_in_place<P>(&mut self, prefix: P) -> bool
       where for<'a> P: UsableReversePattern<'a>;
    fn trim_end_matches_in_place<P>(&mut self, prefix: P)
       where for<'a> P: UsableReversePattern<'a>;
}
Expand description

The trait responsible for adding methods to String.

Required Methods§

source

fn trim_start_bytes(&mut self, amount: usize)

Removes the amount first bytes from the string in bulk.

source

fn trim_end_bytes(&mut self, amount: usize)

Removes the amount last bytes from the string in bulk.

source

fn trim_in_place(&mut self)

Modifies the string to remove leading and trailing whitespace.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space, which includes newlines.

Examples

Basic usage:

let mut s = String::from("\n Hello\tworld\t\n");
s.trim_in_place();

assert_eq!("Hello\tworld", s);
source

fn trim_start_in_place(&mut self)

Modifies the string to remove leading whitespace.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space, which includes newlines.

Text directionality

A string is a sequence of bytes. start in this context means the first position of that byte string; for a left-to-right language like English or Russian, this will be left side, and for right-to-left languages like Arabic or Hebrew, this will be the right side.

Examples

Basic usage:

let mut s = String::from("\n Hello\tworld\t\n");
s.trim_start_in_place();

assert_eq!("Hello\tworld\t\n", s);

Directionality:

let mut s = String::from("  English  ");
s.trim_start_in_place();
assert!(Some('E') == s.chars().next());

let mut s = String::from("  עברית  ");
s.trim_start_in_place();
assert!(Some('ע') == s.chars().next());
source

fn trim_end_in_place(&mut self)

Modifies the string to remove trailing whitespace.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space, which includes newlines.

Text directionality

A string is a sequence of bytes. end in this context means the last position of that byte string; for a left-to-right language like English or Russian, this will be right side, and for right-to-left languages like Arabic or Hebrew, this will be the left side.

Examples

Basic usage:

let mut s = String::from("\n Hello\tworld\t\n");
s.trim_end_in_place();

assert_eq!("\n Hello\tworld", s);

Directionality:

let mut s = String::from("  English  ");
s.trim_end_in_place();

assert!(Some('h') == s.chars().rev().next());

let mut s = String::from("  עברית  ");
s.trim_end_in_place();
assert!(Some('ת') == s.chars().rev().next());
source

fn trim_matches_in_place<P>(&mut self, pat: P)where for<'a> P: UsableDoubleEndedPattern<'a>,

Available on crate feature nightly only.

Modifies a string so all prefixes and suffixes that match a pattern repeatedly are removed.

The pattern can be a char, a slice of chars, or a function or closure that determines if a character matches.

Examples

Simple patterns:

let mut s = String::from("11foo1bar11");
s.trim_matches_in_place('1');
assert_eq!(s, "foo1bar");

let mut s = String::from("123foo1bar123");
s.trim_matches_in_place(char::is_numeric);
assert_eq!(s, "foo1bar");

let mut s = String::from("12foo1bar12");
let x: &[_] = &['1', '2'];
s.trim_matches_in_place(x);
assert_eq!(s, "foo1bar");

A more complex pattern, using a closure:

let mut s = String::from("1foo1barXX");
s.trim_matches_in_place(|c| c == '1' || c == 'X');
assert_eq!(s, "foo1bar");
source

fn trim_start_matches_in_place<P>(&mut self, pat: P)where for<'a> P: Pattern<'a>,

Available on crate feature nightly only.

Modifies a string so all prefixes that match a pattern repeatedly are removed.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

Text directionality

A string is a sequence of bytes. start in this context means the first position of that byte string; for a left-to-right language like English or Russian, this will be left side, and for right-to-left languages like Arabic or Hebrew, this will be the right side.

Examples

Basic usage:

let mut s = String::from("11foo1bar11");
s.trim_start_matches_in_place('1');
assert_eq!(s, "foo1bar11");

let mut s = String::from("123foo1bar123");
s.trim_start_matches_in_place(char::is_numeric);
assert_eq!(s, "foo1bar123");

let mut s = String::from("12foo1bar12");
let x: &[_] = &['1', '2'];
s.trim_start_matches_in_place(x);
assert_eq!(s, "foo1bar12");
source

fn strip_prefix_in_place<P>(&mut self, prefix: P) -> boolwhere for<'a> P: Pattern<'a>,

Available on crate feature nightly only.

Modifies a string so the prefix is removed.

If the string starts with the pattern prefix, this method returns true. Unlike trim_start_matches_in_place, this method removes the prefix exactly once.

If the string does not start with prefix, returns false.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

Examples
let mut s = String::from("foo:bar");
assert!(s.strip_prefix_in_place("foo:"));
assert_eq!(s, "bar");

let mut s = String::from("foo:bar");
assert!(!s.strip_prefix_in_place("bar"));
assert_eq!(s, "foo:bar");

let mut s = String::from("foofoo");
assert!(s.strip_prefix_in_place("foo"));
assert_eq!(s, "foo");
source

fn strip_suffix_in_place<P>(&mut self, prefix: P) -> boolwhere for<'a> P: UsableReversePattern<'a>,

Available on crate feature nightly only.

Modifies a string so the suffix is removed.

If the string ends with the pattern suffix, this method returns true. Unlike trim_end_matches_in_place, this method removes the suffix exactly once.

If the string does not end with suffix, returns false.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

Examples
let mut s = String::from("bar:foo");
assert!(s.strip_suffix_in_place(":foo"));
assert_eq!(s, "bar");

let mut s = String::from("bar:foo");
assert!(!s.strip_suffix_in_place("bar"));
assert_eq!(s, "bar:foo");

let mut s = String::from("foofoo");
assert!(s.strip_suffix_in_place("foo"));
assert_eq!(s, "foo");
source

fn trim_end_matches_in_place<P>(&mut self, prefix: P)where for<'a> P: UsableReversePattern<'a>,

Available on crate feature nightly only.

Modifies a string so all suffixes that match a pattern repeatedly are removed.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

Text directionality

A string is a sequence of bytes. end in this context means the last position of that byte string; for a left-to-right language like English or Russian, this will be right side, and for right-to-left languages like Arabic or Hebrew, this will be the left side.

Examples

Simple patterns:

let mut s = String::from("11foo1bar11");
s.trim_end_matches_in_place('1');
assert_eq!(s, "11foo1bar");

let mut s = String::from("123foo1bar123");
s.trim_end_matches_in_place(char::is_numeric);
assert_eq!(s, "123foo1bar");

let mut s = String::from("12foo1bar12");
let x: &[_] = &['1', '2'];
s.trim_end_matches_in_place(x);
assert_eq!(s, "12foo1bar");

A more complex pattern, using a closure:

let mut s = String::from("1fooX");
s.trim_end_matches_in_place(|c| c == '1' || c == 'X');
assert_eq!(s, "1foo");

Implementations on Foreign Types§

source§

impl StringExtensions for String

source§

fn trim_start_bytes(&mut self, amount: usize)

source§

fn trim_end_bytes(&mut self, amount: usize)

source§

fn trim_in_place(&mut self)

source§

fn trim_start_in_place(&mut self)

source§

fn trim_end_in_place(&mut self)

source§

fn trim_matches_in_place<P>(&mut self, pat: P)where for<'a> P: UsableDoubleEndedPattern<'a>,

Available on crate feature nightly only.
source§

fn trim_start_matches_in_place<P>(&mut self, pat: P)where for<'a> P: Pattern<'a>,

Available on crate feature nightly only.
source§

fn strip_prefix_in_place<P>(&mut self, prefix: P) -> boolwhere for<'a> P: Pattern<'a>,

Available on crate feature nightly only.
source§

fn strip_suffix_in_place<P>(&mut self, prefix: P) -> boolwhere for<'a> P: UsableReversePattern<'a>,

Available on crate feature nightly only.
source§

fn trim_end_matches_in_place<P>(&mut self, pat: P)where for<'a> P: UsableReversePattern<'a>,

Available on crate feature nightly only.

Implementors§