typestar

String basics in C++

std::string owns its characters and hands you concatenation, search, and slicing as methods.

#include <string>

std::string order_label() {
    std::string base = "vanilla";
    std::string order = base + " swirl";
    if (order.find("swirl") != std::string::npos) {
        order += " cone";
    }
    return order.substr(0, base.size()) + ", size " +
           std::to_string(order.size());
}

How it works

  1. + joins strings into a new one; += grows order in place.
  2. find returns a position, or std::string::npos when the needle is missing.
  3. substr(0, n) copies a prefix; std::to_string turns the size() count into text.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
263
Tokens
93
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 74 seconds.

Type this snippet

Step 3 of 3 in Values & references, step 3 of 27 in Language basics.

← Previous Next →