Menu
Coddy logo textTech

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 an Option<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 icon

Challenge

Easy

Create a method named analyzeString that takes a string as input and prints the following analysis using string methods:

  1. The length of the string
  2. The character at index 4
  3. Whether the string contains the substring "Rust"
  4. Whether the string ends with a dot .
  5. 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 characters
  • chars().nth(index).unwrap(): Returns the character at the specified index (0-based)
  • 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
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);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals