pub fn substr(s: &str, start_index: isize, length: isize) -> StringExpand description
Get a substring of a string, beginning at character index ‘start_index’ and take ‘length’ characters. Index of the first character is 0. Negative numbers count backwards: ‘start_index’ from the end of the string. ‘length’ from ‘start_index’. If start_index exceeds the string boundary limits, return an empty string. (Similar to C++ std::substr() and c# String.Substring.) Examples: “0123456789”.substr(2, 3) => “234” “0123456789”.substr(-5, 3) => “567” “0123456789”.substr(-5, -3) => “345” “0123456789”.substr(5, -3) => “345” “0123456789”.substr(2, 0) => “” “0123456789”.substr(0, 0) => “” “0123456789”.substr(-4, 0) => “” To get the characters until the end of the string: “0123456789”.substr(2, isize::MAX) => “23456789” Or substr_end(start_index) “0123456789”.substr_end(2) => “23456789”
Examples found in repository?
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}