Rust String Methods
String Methods
In the Strings chapter, you learned to create strings, add text with push_str(), and get the length with len().
Rust has many more built-in methods. You use them to change, search and clean up text, for example to remove extra spaces from a name that a user typed.
Uppercase and Lowercase
The to_uppercase() and to_lowercase() methods return a new string with all letters changed:
Example
let text = String::from("Hello World");
println!("{}", text.to_uppercase());
println!("{}", text.to_lowercase());
Try it Yourself »
Note: These methods do not change the original string. They return a new String with the result.
Remove Spaces
The trim() method removes spaces at the beginning and the end of a string. The square brackets in this example only show where the text starts and ends:
Example
let name = " Liam ";
println!("[{}]", name);
println!("[{}]", name.trim());
Try it Yourself »
Search in a String
Use contains() to check if a string has a piece of text inside it, starts_with() to check how it begins, and ends_with() to check how it ends. They all return true or false:
Example
let text = String::from("I love Rust");
println!("{}", text.contains("Rust"));
println!("{}", text.starts_with("I"));
println!("{}", text.ends_with("Python"));
Try it Yourself »
Replace Text
The replace() method replaces every match of a text with another text:
Example
let text = String::from("I like cats");
let new_text = text.replace("cats", "dogs");
println!("{}", new_text);
Try it Yourself »
Length in Bytes and Characters
The len() method counts bytes, not characters. For English letters, this is the same, because each letter is one byte. But some characters, like é, use two bytes. To count the characters, use chars().count():
Example
let word = "Café";
println!("{}", word.len());
println!("{}", word.chars().count());
Try it Yourself »
Loop Through Characters
The chars() method gives you the characters of a string one by one, so you can use it in a for loop:
Example
let name = "John";
for c in name.chars() {
println!("{}", c);
}
Try it Yourself »
Split a String
The split() method cuts a string into parts at a separator, like a comma:
Example
let fruits = "apple,banana,cherry";
for fruit in fruits.split(",") {
println!("{}", fruit);
}
Try it Yourself »
Common String Methods
| Method | Description |
|---|---|
to_uppercase() | Returns the string in uppercase letters |
to_lowercase() | Returns the string in lowercase letters |
trim() | Removes spaces at the beginning and the end |
trim_start() | Removes spaces at the beginning |
trim_end() | Removes spaces at the end |
contains(text) | Returns true if the string contains the text |
starts_with(text) | Returns true if the string begins with the text |
ends_with(text) | Returns true if the string ends with the text |
replace(from, to) | Replaces every match of one text with another text |
len() | Returns the length of the string in bytes |
is_empty() | Returns true if the string has no characters |
chars() | Gives the characters of the string one by one |
split(separator) | Splits the string into parts at the separator |
split_whitespace() | Splits the string into words |
repeat(n) | Returns the string repeated n times |