std::string::find locates the first position where a smaller piece of text appears.

find `find` returns the first matching position, or `std::string::npos` when the text is not present.
position String positions are zero-based indexes.

Find Substring

needle
find_substring.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>

int main() {
    std::string needle = "log";

    std::string line = "app.log: ready";
    std::size_t found = line.find(needle);
    int position = found == std::string::npos ? -1 : static_cast<int>(found);

    std::cout << "needle=" << needle << std::endl;
    std::cout << "position=" << position << std::endl;
    return 0;
}
#include <iostream>
#include <string>

int main() {
    std::string needle = "err";

    std::string line = "app.log: ready";
    std::size_t found = line.find(needle);
    int position = found == std::string::npos ? -1 : static_cast<int>(found);

    std::cout << "needle=" << needle << std::endl;
    std::cout << "position=" << position << std::endl;
    return 0;
}
#include <iostream>
#include <string>

int main() {
    std::string needle = "zip";

    std::string line = "app.log: ready";
    std::size_t found = line.find(needle);
    int position = found == std::string::npos ? -1 : static_cast<int>(found);

    std::cout << "needle=" << needle << std::endl;
    std::cout << "position=" << position << std::endl;
    return 0;
}
  1. needle ← log, line ← app.log: ready, found ← 4, position ← 4

    4int main() {5    std::string needle→ log = "log"; //@needle="err", "zip"67    std::string line→ app.log: ready = "app.log: ready";8    std::size_t found→ 4 = lineapp.log: ready.find(needlelog);9    int position→ 4 = found4 == std::string::npos ? -1 : static_cast<int>(found);1011    std::cout << "needle=" << needlelog << std::endl;12    std::cout << "position=" << position4 << std::endl;13    return 0;14}
    outputneedle=log
    position=4
  1. needle ← err, line ← app.log: ready, found ← 18446744073709551615

    4int main() {5    std::string needle→ err = "err";67    std::string line→ app.log: ready = "app.log: ready";8    std::size_t found→ 18446744073709551615 = lineapp.log: ready.find(needleerr);9    int position→ -1 = found18446744073709551615 == std::string::npos ? -1 : static_cast<int>(found);1011    std::cout << "needle=" << needleerr << std::endl;12    std::cout << "position=" << position-1 << std::endl;13    return 0;14}
    outputneedle=err
    position=-1
  1. needle ← zip, line ← app.log: ready, found ← 18446744073709551615

    4int main() {5    std::string needle→ zip = "zip";67    std::string line→ app.log: ready = "app.log: ready";8    std::size_t found→ 18446744073709551615 = lineapp.log: ready.find(needlezip);9    int position→ -1 = found18446744073709551615 == std::string::npos ? -1 : static_cast<int>(found);1011    std::cout << "needle=" << needlezip << std::endl;12    std::cout << "position=" << position-1 << std::endl;13    return 0;14}
    outputneedle=zip
    position=-1