Trait substring_replace::SubstringReplace

source ·
pub trait SubstringReplace {
    // Required methods
    fn substring(&self, start: usize, end: usize) -> &str;
    fn substring_replace(
        &self,
        replacement: &str,
        start: usize,
        end: usize,
    ) -> String;
    fn to_start_byte_index(&self, start: usize) -> usize;
    fn to_end_byte_index(&self, start: usize) -> usize;
    fn char_len(&self) -> usize;

    // Provided methods
    fn substring_start(&self, end: usize) -> &str { ... }
    fn substring_end(&self, start: usize) -> &str { ... }
    fn substring_replace_start(&self, replacement: &str, end: usize) -> String { ... }
    fn substring_replace_end(&self, replacement: &str, start: usize) -> String { ... }
    fn substring_offset(&self, position: usize, length: i32) -> &str { ... }
    fn substring_insert(&self, replacement: &str, start: usize) -> String { ... }
}

Required Methods§

source

fn substring(&self, start: usize, end: usize) -> &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( &self, replacement: &str, start: usize, end: usize, ) -> 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

Provided Methods§

source

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

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

source

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

Return a substring from a specified start character index to a specified end If start index is greater than the max character index, the function will yield an empty string

source

fn substring_replace_start(&self, replacement: &str, end: usize) -> 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”

source

fn substring_replace_end(&self, replacement: &str, start: usize) -> 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”

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_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 also works directly with &str, while returning a new owned string

Implementations on Foreign Types§

source§

impl SubstringReplace for str

source§

fn substring(&self, start: usize, end: usize) -> &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( &self, replacement: &str, start: usize, end: usize, ) -> String

Replace

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

Implementors§