1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use super::StrHelper;
impl StrHelper {
/// insert element at string.
///
/// # Example:
/// ```
/// #[macro_use] extern crate std_helper;
/// use std_helper::StrHelper;
///
/// let mut helper = str!("Hello!");
/// let result = helper.insert(", World", 5).unwrap();
///
/// assert_eq!(result, "Hello, World!")
/// ```
pub fn insert(&mut self, new_part: &str, index: usize) -> Option<String> {
let (part1, part2) = self
.split_str_at(index);
let mut new_string = part1?
.to_string();
for ch in StrHelper::other_as_chars(new_part) {
new_string.push(ch);
}
for ch in StrHelper::other_as_chars(part2?) {
new_string.push(ch);
}
self.string = new_string;
Some(self.string.clone())
}
/// insert element at the beginning of string.
///
/// # Example:
/// ```
/// #[macro_use] extern crate std_helper;
/// use std_helper::StrHelper;
///
/// let mut helper = str!("World!");
/// let result = helper.insert_at_the_beginning("Hello, ").unwrap();
///
/// assert_eq!(result, "Hello, World!")
/// ```
pub fn insert_at_the_beginning(&mut self, new_part: &str) -> Option<String> {
let old_string = self.string.clone();
self.string = new_part.to_string();
for ch in StrHelper::other_as_chars(old_string.as_str()) {
self.string.push(ch);
}
Some(self.string.clone())
}
/// insert element at the end of string.
///
/// # Example:
/// ```
/// #[macro_use] extern crate std_helper;
/// use std_helper::StrHelper;
///
/// let mut helper = str!("Hello");
/// let result = helper.push(", World!").unwrap();
///
/// assert_eq!(result, "Hello, World!")
/// ```
pub fn push(&mut self, new_part: &str) -> Option<String> {
for ch in StrHelper::other_as_chars(new_part) {
self.string.push(ch);
}
Some(self.string.clone())
}
}