Trait SubstringReplace

Source
pub trait SubstringReplace
where Self: ToString,
{
Show 25 methods // Required methods fn substring<T: ToOffset>(&self, start: usize, end: T) -> &str; fn substring_replace<T: ToOffset>( &self, replacement: &str, start: usize, end: T, ) -> String; fn to_start_byte_index(&self, start: usize) -> usize; fn to_end_byte_index(&self, start: usize) -> usize; fn char_len(&self) -> usize; fn char_find(&self, pat: &str) -> Option<usize>; fn char_rfind(&self, pat: &str) -> Option<usize>; fn insert_adjacent( &self, insert: &str, pat: &str, before: bool, first: bool, ) -> String; // Provided methods fn substring_start(&self, end: i64) -> &str { ... } fn substring_end(&self, start: i64) -> &str { ... } fn substring_range(&self, start: usize, end: i64) -> &str { ... } fn substring_replace_range( &self, replacement: &str, start: usize, end: i64, ) -> String { ... } fn substring_replace_start(&self, replacement: &str, end: i64) -> String { ... } fn substring_replace_end(&self, replacement: &str, start: i64) -> String { ... } fn substring_remove(&self, start: usize, end: usize) -> String { ... } fn substring_offset(&self, position: usize, length: i32) -> &str { ... } fn substring_pull(&self, position: usize, length: i32) -> String { ... } fn substring_insert(&self, replacement: &str, start: usize) -> String { ... } fn insert_before_first(&self, insert: &str, pat: &str) -> String { ... } fn insert_before_last(&self, insert: &str, pat: &str) -> String { ... } fn insert_after_first(&self, insert: &str, pat: &str) -> String { ... } fn insert_after_last(&self, insert: &str, pat: &str) -> String { ... } fn insert_between( &self, insert: &str, start_pat: &str, end_pat: &str, ) -> String { ... } fn prepend(&self, insert: &str) -> String { ... } fn append(&self, insert: &str) -> String { ... }
}
Expand description

Trait with extension methods to manipulate substrings by character indices behaving like similar methods in other languages

Required Methods§

Source

fn substring<T: ToOffset>(&self, start: usize, end: T) -> &str

Return a substring by start and end character index With multibyte characters this will not be the same as the byte indices used by str slices

Source

fn substring_replace<T: ToOffset>( &self, replacement: &str, start: usize, end: T, ) -> String

Source

fn to_start_byte_index(&self, start: usize) -> usize

Convert character index to start byte index

Source

fn to_end_byte_index(&self, start: usize) -> usize

Convert character index to end byte index

Source

fn char_len(&self) -> usize

Return the character length rather than the byte length

Source

fn char_find(&self, pat: &str) -> Option<usize>

Return the character index rather than the byte index of the first match of a pattern

Source

fn char_rfind(&self, pat: &str) -> Option<usize>

Return the character index rather than the byte index of the last match of a pattern this will be first index of the match

Source

fn insert_adjacent( &self, insert: &str, pat: &str, before: bool, first: bool, ) -> String

Insert before or after the first or last occurrence

Provided Methods§

Source

fn substring_start(&self, end: i64) -> &str

Return a substring from the start and to a specified end character index

Source

fn substring_end(&self, start: i64) -> &str

Return a substring from a specified start character index to a specified end A negative offset represents character index from the end, e.g. if character length is 15, -5 translates to 10 If start index is greater than the max character index, the function will yield an empty string

Source

fn substring_range(&self, start: usize, end: i64) -> &str

👎Deprecated since 0.2.1: Use substring instead

Return a substring by start and end character index Unlike the default substring() method, the end index may be negative, in which case it counts backwards from the end, e.g. if character length is 15, -5 translates to 10

Source

fn substring_replace_range( &self, replacement: &str, start: usize, end: i64, ) -> String

👎Deprecated since 0.2.1: Use substring instead

Replace substring delimited by start and end character index Unlike the default substring_replace() method, the end index may be negative, in which case it counts backwards from the end, e.g. if character length is 15, -5 translates to 10

Source

fn substring_replace_start(&self, replacement: &str, end: i64) -> String

Replace the start of a string to specified end character index e.g. “brown”.substring_replace_start(“d”, 2); will replace the first two characters with “d”, yield “down” A negative offset represents character index from the end, e.g. if character length is 15, -5 translates to 10

Source

fn substring_replace_end(&self, replacement: &str, start: i64) -> String

Replace the remainder of string from a specified start character index e.g. “blue”.substring_replace_last(“ack”, 2); will replace the last 2 characters with “ack”, yielding “black” A negative offset represents character index from the end, e.g. if character length is 15, -5 translates to 10

Source

fn substring_remove(&self, start: usize, end: usize) -> String

Remove a string delimited by a start and end character index e.g. “abcde”.substring_remove(2, 4); will remove characters with indices of 2 and 3 (3rd and 4th or c and d) resulting in “abe”, i.e. the opposite behaviour to substring()

Source

fn substring_offset(&self, position: usize, length: i32) -> &str

Extract a substring from a start index for n characters to the right A negative length in the second parameter will start at the start index

Source

fn substring_pull(&self, position: usize, length: i32) -> String

Remove a string from a start position to given length negative lengths will remove characters to the left e.g. “abcde”.substring_remove(3, -3); will remove characters with indices of 1 and 2 (2nd and 3rd or b and c) resulting in “ade”, i.e. the opposite behaviour to substring_offset()

Source

fn substring_insert(&self, replacement: &str, start: usize) -> String

Insert a string at a given character index This differs from String::insert by using character rather than byte indices to work better with multibyte characters It’s also implemented for &str, while returning a new owned string

Source

fn insert_before_first(&self, insert: &str, pat: &str) -> String

Insert before the first occurrence of a string

Source

fn insert_before_last(&self, insert: &str, pat: &str) -> String

Insert before the first occurrence of a string

Source

fn insert_after_first(&self, insert: &str, pat: &str) -> String

Insert after the last occurrence of a string

Source

fn insert_after_last(&self, insert: &str, pat: &str) -> String

Insert after the last occurrence of a string

Source

fn insert_between(&self, insert: &str, start_pat: &str, end_pat: &str) -> String

Insert between the first occurrence of a one string and the last occurrence of another

Source

fn prepend(&self, insert: &str) -> String

Insert between the first occurrence of a one string and the last occurrence of another

Source

fn append(&self, insert: &str) -> String

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl SubstringReplace for str

Source§

fn substring<T: ToOffset>(&self, start: usize, end: T) -> &str

Extract substring by character indices and hand overflow gracefully if the end index is equal or greater than start index, the function will yield an empty string

Source§

fn substring_replace<T: ToOffset>( &self, replacement: &str, start: usize, end: T, ) -> String

Replace a segment delimited by start and end characters indices with a string pattern (&str)

Source§

fn to_start_byte_index(&self, start: usize) -> usize

Translate the character start index to the start byte index to avoid boundary collisions with multibyte characters

Source§

fn to_end_byte_index(&self, end: usize) -> usize

Translate the character end index to the end byte index to avoid boundary collisions with multibyte characters

Source§

fn char_len(&self) -> usize

Return the character length as opposed to the byte length This will differ from len() only multibyte characters

Source§

fn char_find(&self, pat: &str) -> Option<usize>

Return the character index of the first match of a given pattern

Source§

fn char_rfind(&self, pat: &str) -> Option<usize>

Return the character index rather than the byte index of the last match of a pattern this will be first index of the match

Source§

fn insert_adjacent( &self, insert: &str, pat: &str, before: bool, first: bool, ) -> String

Insert before or after the first or last occurrence

Implementors§