pub fn substring(s: &str, start_index: isize, end_index: isize) -> StringExpand description
Get a substring of a string beginning at character index start_index up to and excluding the character index end_index. Equivalent of JavaScript substring with 2 parameters. If start_index is equal to end_index, substring() returns an empty string. If start_index is greater than end_index, swap start_index and end_index. Any argument value that is less than 0 is treated as if it were 0. Any argument value that is greater than string length is treated as if it were string length. Index of the first character is 0.
Examples found in repository?
examples/main.rs (line 21)
4fn main() {
5 // let s1: &str = "test éèçà 123 test";
6 // let s2: String = s1.to_owned();
7
8 let s1: &str = "0123456789";
9 let s2: String = s1.to_owned();
10
11 println!("substr: {}", substr(s1, 3, 2)); // "34"
12 println!("substr: {}", substr(&s2, 3, 2)); // "34"
13 println!("substr: {}", s1.substr(3, 2)); // "34"
14 println!("substr: {}", s2.substr(3, 2)); // "34"
15 println!("substr: {}", s2.substr(5, isize::MAX)); // "56789"
16
17 println!("substru: {}", substru(s1, 3, 2)); // "34"
18 println!("substru: {}", s1.substru(3, 2)); // "34"
19 println!("substru: {}", s2.substru(3, 2)); // "34"
20
21 println!("substring: {}", substring(s1, 3, 5)); // "34"
22 println!("substring: {}", s1.substring(3, 5)); // "34"
23
24 println!("substr: {}", substr(s1, -2, 2)); // "89", last 2 characters
25
26 println!("substr: {}", substr(s1, -10, 1)); // 0
27 println!("substr: {}", substr(s1, 0, 1)); // 0
28
29 println!("substr: {}", substr(s1, 6, isize::MAX)); // "6789"
30
31 println!("substr_end: {}", substr_end(s1, 6)); // "6789", last 2 characters
32 println!("substr_end: {}", s1.substr_end(6)); // "6789", last 2 characters
33
34 println!("str_remove: {}", str_remove(s1, 3, 2)); // "01256789"
35 println!("str_remove: {}", s1.str_remove(3, 2)); // "01256789"
36
37 match s1.indexof("234", 0) {
38 // Result: Some(2)
39 Some(pos) => println!("Found at position: {}", pos),
40 None => println!("Not found"),
41 }
42
43 println!("str_concat: {}", str_concat!(s1, " ", &s2)); // "0123456789 0123456789"
44 println!("str_concat: {}", str_concat!(s1, " ", s1)); // "0123456789 0123456789"
45}