rufl/string/after_last.rs
1/// Returns the substring after the last occurrence of a specified `substr` in the source string.
2///
3/// # Arguments
4///
5/// * `s` - The input string to perform after_last operation.
6/// * `substr` - The substring to look for last occurrence in `s`.
7///
8/// # Returns
9///
10/// Returns the substring after the last occurrence of `substr` in `s`.
11///
12/// # Examples
13///
14/// ```
15/// use rufl::string;
16///
17/// let foo = string::after_last("foo", "o");
18/// assert_eq!("", foo);
19///
20/// let bar = string::after_last("bar", "a");
21/// assert_eq!("r", bar);
22///
23/// let boo = string::after_last("boo", "c");
24/// assert_eq!("boo", boo);
25///
26/// ```
27
28pub fn after_last(s: impl AsRef<str>, substr: &str) -> String {
29 match s.as_ref().rfind(substr) {
30 Some(index) => s.as_ref()[index + substr.len()..].to_string(),
31 None => s.as_ref().to_string(),
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn test_after_last() {
41 assert_eq!("", after_last("foo", ""));
42 assert_eq!("", after_last("foo", "o"));
43 assert_eq!("", after_last("foo", "foo"));
44 assert_eq!("foo", after_last("foo", "a"));
45 assert_eq!("boo", after_last("foo/bar/boo", "/"));
46
47 assert_eq!("rust", after_last("你好c++你好rust", "你好"));
48 assert_eq!(
49 " programátor",
50 after_last("Jsem počítačový programátor", "počítačový")
51 );
52 }
53}