Skip to main content

remove_char

Function remove_char 

Source
pub fn remove_char(
    remove_from: RemoveLocationEnum,
    input: &String,
    target: char,
) -> String
Expand description

Removes the first or last character of a string if it matches the given target character.

§Arguments

  • begin - A boolean indicating whether to remove the first (true) or last (false) character.
  • input - A String to process.
  • target - The character to remove.

§Returns

Returns a new String with the character removed if it matched.

§Examples

use bt_string_utils::{remove_char, RemoveLocationEnum};
let modified = remove_char(RemoveLocationEnum::Begin, &"hello".to_string(), 'h');
assert_eq!(modified, "ello");

let modified = remove_char(RemoveLocationEnum::End, &"world!".to_string(), '!');
assert_eq!(modified, "world");

If the character doesn’t match, the original string is returned:

use bt_string_utils::{remove_char, RemoveLocationEnum};
let modified = remove_char(RemoveLocationEnum::Begin, &"rust".to_string(), 'x');
assert_eq!(modified, "rust");