上QQ阅读APP看书,第一时间看更新
How to do it...
- In the folder src/bin, create a file called string.rs.
- Add the following code, and run it with cargo run --bin string:
Use the following code to split a string in various ways:
42 // Split a string slice into two halves
43 let (first, second) = "HelloThere".split_at(5);
44 println!("first: {}, second: {}", first, second);
45
46 // Split on individual lines
47 let haiku = "\
48 she watches\n\
49 satisfied after love\n\
50 he lies\n\
51 looking up at nothing\n\
52 ";
53 for line in haiku.lines() {
54 println!("\t{}.", line);
55 }
56
57 // Split on substrings
58 for s in "Never;Give;Up".split(';') {
59 println!("{}", s);
60 }
61 // When the splitted string is at the beginning or end,
62 // it will result in the empty string
63 let s: Vec<_> = "::Hi::There::".split("::").collect();
64 println!("{:?}", s);
65
66 // If you can eliminate the empty strings at the end
67 // by using split_termitor
68 let s: Vec<_> = "Mr. T.".split_terminator('.').collect();
69 println!("{:?}", s);
70
71 // char has a few method's that you can use to split on
72 for s in "I'm2fast4you".split(char::is_numeric) {
73 println!("{}", s);
74 }
75
76 // Split only a certain amount of times
77 for s in "It's not your fault, it's mine".splitn(3,
char::is_whitespace) {
78 println!("{}", s);
79 }
80
81 // Get only the substrings that match a pattern
82 // This is the opposite of splitting
83 for c in "The Dark Knight rises".matches(char::is_uppercase) {
84 println!("{}", c);
85 }
86
87 // Check if a string starts with something
88 let saying = "The early bird gets the worm";
89 let starts_with_the = saying.starts_with("The");
90 println!(
"Does \"{}\" start with \"The\"?: {}",
saying,
starts_with_the
);
91 let starts_with_bird = saying.starts_with("bird");
92 println!(
"Does \"{}\" start with \"bird\"?: {}",
saying,
starts_with_bird
);
93
94 // Check if a string ends with something
95 let ends_with_worm = saying.ends_with("worm");
96 println!("Does \"{}\" end with \"worm\"?: {}", saying,
ends_with_worm);
97
98 // Check if the string contains something somewhere
99 let contains_bird = saying.contains("bird");
100 println!("Does \"{}\" contain \"bird\"?: {}", saying,
contains_bird);
Remove whitespace:
105 // Splitting on whitespace might not result in what you expect
106 let a_lot_of_whitespace = " I love spaaace ";
107 let s: Vec<_> = a_lot_of_whitespace.split(' ').collect();
108 println!("{:?}", s);
109 // Use split_whitespace instead
110 let s: Vec<_> =
a_lot_of_whitespace.split_whitespace().collect();
111 println!("{:?}", s);
112
113 // Remove leading and trailing whitespace
114 let username = " P3ngu1n\n".trim();
115 println!("{}", username);
116 // Remove only leading whitespace
117 let username = " P3ngu1n\n".trim_left();
118 println!("{}", username);
119 // Remove only trailing whitespace
120 let username = " P3ngu1n\n".trim_right();
121 println!("{}", username);
122
123
124 // Parse a string into another data type
125 // This requires type annotation
126 let num = "12".parse::<i32>();
127 if let Ok(num) = num {
128 println!("{} * {} = {}", num, num, num * num);
129 }
Modify the string:
133 // Replace all occurences of a pattern
134 let s = "My dad is the best dad";
135 let new_s = s.replace("dad", "mom");
136 println!("new_s: {}", new_s);
137
138 // Replace all characters with their lowercase
139 let lowercase = s.to_lowercase();
140 println!("lowercase: {}", lowercase);
141
142 // Replace all characters with their uppercase
143 let uppercase = s.to_uppercase();
144 println!("uppercase: {}", uppercase);
145
146 // These also work with other languages
147 let greek = "?ΔΥΣΣΕ?Σ";
148 println!("lowercase greek: {}", greek.to_lowercase());
149
150 // Repeat a string
151 let hello = "Hello! ";
152 println!("Three times hello: {}", hello.repeat(3));
153 }