Splitting a string

How to split a string based on specific separators?

    By separator - s.split("separator")
    By whitespace - s.split_whitespace()
    By newlines - s.lines()

How to split a string based on multiple characters?

Standard library split can be used with a closure to fulfill this use case.
1
fn split_stuff() {
2
let message = "Hello there | how are you ; doing?";
3
for s in message.split(|c| (c == '|') || (c == ';')) {
4
dbg!(s);
5
}
6
}
Copied!
Output would be as follows,
s = "Hello there " s = " how are you " s = " doing?"
To get rid of spaces, trim() method can be used.
Although it is possible to write a closure to split on arbitrarily complex conditions, it easier to use regex crate for splitting based on regular expressions.
Another approach without using a closure would be to make use of string slice patterns. As an example, we can write the above split as,
1
for s in message.split(&['|', ';'][..]) {
2
// ...
3
}
Copied!
Splits can be collected to a vector and other types of Rust collections with collect method.

How to split a string based on regular expressions?

Splitting a string such as "hello there; how| are|you, chathura" based on multiple characters and taking into account multiple spaces is not simple without using regular expressions.
Following solution demonstrates the use of regex crate to accomplish the task.
1
fn split_by_regex() {
2
let message = "hello there; how| are|you, Chathura";
3
​
4
let re = regex::Regex::new(r"[;,|\s]\s*").unwrap();
5
​
6
for s in re.split(message) {
7
dbg!(s);
8
}
9
}
Copied!
Last modified 1yr ago