String Methods
Part of the Fundamentals section of Coddy's Rust journey — lesson 69 of 75.
Rust has a rich set of built-in string methods that allow you to perform various operations on strings. These methods provide functionalities for manipulating, searching, and transforming strings. Here are some commonly used string methods:
len(): Returns the number of characters in the string.chars().nth(index): Returns the character at the specified index (0-based). This method returns anOption<char>— a special Rust type that represents either Some value (if the index exists) or None (if it doesn't). Calling.unwrap()on it extracts the character value directly.
contains(substring): Checks if the string contains the specified substring.starts_with(prefix): Checks if the string starts with the specified prefix.ends_with(suffix): Checks if the string ends with the specified suffix.
to_uppercase(): Converts the string to uppercase.to_lowercase(): Converts the string to lowercase.trim(): Removes leading and trailing whitespace from the string.
For example:
let message = "Hello, World!";
let length = message.len();
let firstChar = message.chars().nth(0).unwrap();
let contains_world = message.contains("World");
let starts_with_hello = message.starts_with("Hello");
let lower = message.to_lowercase();Challenge
EasyCreate a method named analyzeString that takes a string as input and prints the following analysis using string methods:
- The length of the string
- The character at index 4
- Whether the string contains the substring "Rust"
- Whether the string ends with a dot
. - The string converted to uppercase
The output should follow this exact format:
Length: [The length]
Char at 4: [The Char at index 4]
Contains Rust: [true or false]
Ends with dot: [true or false]
Uppercase: [The string in uppercase]Note: Assume the input string will be long enough for all operations.
Cheat sheet
Rust built-in string methods:
len(): Returns the number of characterschars().nth(index).unwrap(): Returns the character at the specified index (0-based)contains(substring): Checks if the string contains the specified substringstarts_with(prefix): Checks if the string starts with the specified prefixends_with(suffix): Checks if the string ends with the specified suffixto_uppercase(): Converts the string to uppercaseto_lowercase(): Converts the string to lowercasetrim(): Removes leading and trailing whitespace
let message = "Hello, World!";
let length = message.len();
let firstChar = message.chars().nth(0).unwrap();
let contains_world = message.contains("World");
let starts_with_hello = message.starts_with("Hello");
let lower = message.to_lowercase();Try it yourself
use std::io;
fn analyze_string(s: &str) {
// Write your code here
}
fn main() {
let mut message = String::new();
io::stdin().read_line(&mut message).unwrap();
let message = message.trim();
analyze_string(message);
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input