#include#include void parseString(std::string_view str) { if (str.starts_with("https://")) { std::string_view domain = str.substr(8); std::cout << "Domain: " << domain << std::endl; } } int main() { std::string url = "https://www.google.com"; parseString(url); return 0; }
#includeIn this example, we use `StringPiece` to tokenize a string by a delimiter character. We search for the delimiter character using `find` and then extract the substring using `substr`. We keep track of the start position using a variable and continue until all the tokens are extracted. This code uses the C++ Standard Library.#include void tokenizeString(std::string_view str, char delim) { size_t start = 0, end; while ((end = str.find(delim, start)) != std::string_view::npos) { std::string_view token = str.substr(start, end - start); std::cout << token << std::endl; start = end + 1; } std::string_view lastToken = str.substr(start); std::cout << lastToken << std::endl; } int main() { std::string sentence = "The quick brown fox jumps over the lazy dog"; tokenizeString(sentence, ' '); return 0; }