rustHow do I split strings in Rust?
Strings in Rust can be split using the split()
method. This method takes a string and a delimiter as parameters and returns an iterator of strings.
Example code
let my_string = "Hello, World!";
let split_string = my_string.split(",");
Output example
["Hello", " World!"]
Code explanation
let my_string = "Hello, World!";
: This line declares a string variable calledmy_string
and assigns it the value"Hello, World!"
.let split_string = my_string.split(",");
: This line calls thesplit()
method on themy_string
variable, passing in the delimiter","
as a parameter. This returns an iterator of strings.
Helpful links
More of Rust
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to clear a Rust HashMap?
- Yield example in Rust
- Example of yield_now in Rust?
- How to convert a Rust slice to a fixed array?
- How to use regex to match a group in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to yield a thread in Rust?
- How to replace strings using Rust regex?
See more codes...