DMarlon Studio

Lesson 4 · 4 of 20

Text strings · C++

Work with std::string in C++: join with +, measure with size(), cut with substr, search with find and convert between text and numbers.

C++ · Text strings in DMarlon Studio: the example's code and its output in the terminal

`std::string` holds text and knows its length: full.size() gives 12, the characters in Grace Hopper including the space. The `+` operator joins strings, and first + " " + last builds the full name.

`[]` reads one character by its position, starting at 0: first[0] is 'G'. `substr(start, length)` copies a piece: code.substr(0, 2) gives ES and, without a length, code.substr(8) goes to the end: MAD.

`find()` looks for some text and returns its position: 2024 starts at 3. If it finds nothing, it returns the special value `std::string::npos`, which is why code.find("BCN") != std::string::npos is 0, that is, false.

In C++ a string can be changed in place: += adds text at the end and replace(0, 5, "Parcel") swaps the first 5 characters for something else.

To turn text into a number there are `std::stoi` (whole numbers) and `std::stod` (decimals). `std::to_string` goes the other way, so "Units: " + std::to_string(units) can be joined with +.

Code · main.cpp

#include <iostream>#include <string>int main() {    std::string first = "Grace";    std::string last = "Hopper";    // + joins strings; size() counts the characters.    std::string full = first + " " + last;    std::cout << full << " has " << full.size() << " characters\n";    // [] reads one character by position (the first one is 0).    std::cout << "Initials: " << first[0] << last[0] << '\n';    // substr(start, length) copies a part; without length, up to the end.    std::string code = "ES-2024-MAD";    std::cout << "Country: " << code.substr(0, 2) << ", city: " << code.substr(8) << '\n';    // find() returns the position, or std::string::npos if it is missing.    std::cout << "The year starts at position " << code.find("2024") << '\n';    std::cout << "BCN found? " << (code.find("BCN") != std::string::npos) << '\n';    // A std::string can grow and be changed in place.    std::string message = "Order";    message += " shipped";    message.replace(0, 5, "Parcel");    std::cout << message << '\n';    // Converting between text and numbers.    int units = std::stoi("12");    double price = std::stod("2.5");    std::cout << "Units: " + std::to_string(units) << ", total: " << units * price << '\n';    return 0;}

Output · c++ -std=c++17 main.cpp -o main && ./main

Grace Hopper has 12 characters
Initials: GH
Country: ES, city: MAD
The year starts at position 3
BCN found? 0
Parcel shipped
Units: 12, total: 30

Key points

  • + joins strings and size() counts their characters.
  • s[0] is the first character.
  • substr(start, length) copies a piece.
  • find() returns std::string::npos when it finds nothing.
  • std::stoi, std::stod and std::to_string convert between text and numbers.

Exercise

With std::string email = "lucia.perez@example.com";, split the user name from the domain using find('@') and substr, and print how many characters each part has.