Trait substring_replace::SubstringReplace
source · pub trait SubstringReplace {
Show 15 methods
// 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;
fn char_find(&self, pat: &str) -> Option<usize>;
fn char_rfind(&self, pat: &str) -> Option<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_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 { ... }
}
Expand description
Trait with extension methods to manipulate substrings by character indices behaving like similar methods in other languages
Required Methods§
sourcefn substring(&self, start: usize, end: usize) -> &str
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
fn substring_replace( &self, replacement: &str, start: usize, end: usize, ) -> String
sourcefn to_start_byte_index(&self, start: usize) -> usize
fn to_start_byte_index(&self, start: usize) -> usize
Convert character index to start byte index
sourcefn to_end_byte_index(&self, start: usize) -> usize
fn to_end_byte_index(&self, start: usize) -> usize
Convert character index to end byte index
sourcefn char_find(&self, pat: &str) -> Option<usize>
fn char_find(&self, pat: &str) -> Option<usize>
Return the character index rather than the byte index of the first match of a pattern
sourcefn char_rfind(&self, pat: &str) -> Option<usize>
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
Provided Methods§
sourcefn substring_start(&self, end: usize) -> &str
fn substring_start(&self, end: usize) -> &str
Return a substring from the start and to a specified end character index
sourcefn substring_end(&self, start: usize) -> &str
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
sourcefn substring_replace_start(&self, replacement: &str, end: usize) -> String
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”
sourcefn substring_replace_end(&self, replacement: &str, start: usize) -> String
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”
sourcefn substring_remove(&self, start: usize, end: usize) -> String
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()
sourcefn substring_offset(&self, position: usize, length: i32) -> &str
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
sourcefn substring_pull(&self, position: usize, length: i32) -> String
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()
sourcefn substring_insert(&self, replacement: &str, start: usize) -> String
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
Implementations on Foreign Types§
source§impl SubstringReplace for str
impl SubstringReplace for str
source§fn substring(&self, start: usize, end: usize) -> &str
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
fn substring_replace( &self, replacement: &str, start: usize, end: usize, ) -> 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
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
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
fn char_len(&self) -> usize
Return the character length as opposed to the byte length This will differ from len() only multibyte characters