Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Part of a String

Often you need only a part of a string: the first name from a full name, or the time from a line of a chat. The method substr() (short for substring, a part of a string) returns one. It takes two numbers: the index where the part starts, and the number of characters, its length:

std::string weapon = "Iron Sword";
std::cout << weapon.substr(0, 4) << std::endl;
std::cout << weapon.substr(5, 3) << std::endl;
std::cout << weapon.substr(5) << std::endl;
std::cout << weapon << std::endl;

This prints:

Iron
Swo
Sword
Iron Sword

The second number is the length of the part, not the index where it ends. weapon.substr(5, 3) takes three characters starting at index 5, the ones at the indexes 5, 6 and 7. Without the second number, the part goes all the way to the end. And as the last line shows, substr() doesn't change the original string: it returns a new one, which you can print or store in a variable.

What if the numbers don't fit the string? "Iron Sword" has 10 characters, at the indexes 0 to 9:

  • When the part would reach past the end, you get the characters there are: weapon.substr(5, 100) is "Sword".
  • A start right at the end, weapon.substr(10), gives an empty string, "".
  • A start past the end stops the program:
std::string weapon = "Iron Sword";
std::cout << weapon.substr(20) << std::endl;
std::cout << "End" << std::endl;

The program never prints End. Its output ends with an error instead:

std::out_of_range: basic_string
RuntimeError: unreachable

It's the same out_of_range error that at() gives for an index outside a vector. basic_string is the name the library uses for std::string inside.

Instructions

  1. 1

    Each line of the game's chat starts with the time in square brackets. The time starts at index 1 and has 5 characters. Below the print line, store it in a variable time with substr(), and print it: Time: 18:05. The test also runs your program with another chat line.

  2. 2

    The message starts at index 8, after the closing bracket and the space, and goes to the end of the line. Store it in a variable message with substr() and only one number, and print it: Message: Who wants to fight the dragon?.

Back

2/11

  1. 1. Building a String
  2. 2. Part of a String
  3. 3. Searching in a String
  4. 4. Splitting a Command
  5. 5. Quiz: Parts of a String
  6. 6. Alphabetical Order
  7. 7. Uppercase, Lowercase and Digits
  8. 8. Changing a String
  9. 9. Numbers in Strings
  10. 10. Quiz: Buying Potions
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.