rust_string_utils/utils/
remove_delete.rs

1
2/// Removes all occurrences of the specified substring from the given string.
3///
4/// # Arguments
5///
6/// * `str` - The `String` from which to remove the substring.
7/// * `remove` - The `String` to be removed.
8///
9/// # Returns
10///
11/// A new `String` with all occurrences of the specified substring removed.
12pub fn remove(str: &String, remove: &String ) -> String {
13    str.replace(remove, "")
14}
15
16/// Removes all whitespace characters from the given string.
17///
18/// # Arguments
19///
20/// * `str` - The `String` from which to remove whitespace characters.
21///
22/// # Returns
23///
24/// A new `String` with all whitespace characters removed.
25pub fn delete_white_space(str: &String) -> String {
26    str.replace(" ", "")
27}
28
29
30/// Removes all occurrences of the specified substring from the given string, ignoring case.
31///
32/// # Arguments
33///
34/// * `str` - The `String` from which to remove the substring.
35/// * `remove` - The `String` to be removed.
36///
37/// # Returns
38///
39/// A new `String` with all occurrences of the specified substring removed, ignoring case.
40pub fn remove_ignore_case(str: &String, remove: &String) -> String {
41    str.to_lowercase().replace(&remove.to_lowercase(), "")
42}
43
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_remove() {
51        assert_eq!(remove(&"abcdef".to_string(), &"c".to_string()), "abdef");
52        assert_eq!(remove(&"abcdef".to_string(), &"z".to_string()), "abcdef");
53        assert_eq!(remove(&"abcdef".to_string(), &"".to_string()), "abcdef");
54        assert_eq!(remove(&"".to_string(), &"a".to_string()), "");
55    }
56
57    #[test]
58    fn test_delete_white_space() {
59        assert_eq!(delete_white_space(&"a b c".to_string()), "abc");
60        assert_eq!(delete_white_space(&"a  b  c".to_string()), "abc");
61        assert_eq!(delete_white_space(&" abc ".to_string()), "abc");
62        assert_eq!(delete_white_space(&"".to_string()), "");
63    }
64
65    #[test]
66    fn test_remove_ignore_case() {
67        assert_eq!(remove_ignore_case(&"abcdef".to_string(), &"C".to_string()), "abdef");
68        assert_eq!(remove_ignore_case(&"abcdef".to_string(), &"E".to_string()), "abcdf");
69        assert_eq!(remove_ignore_case(&"abcdef".to_string(), &"".to_string()), "abcdef");
70        assert_eq!(remove_ignore_case(&"".to_string(), &"A".to_string()), "");
71    }
72}