Library contents

C++ std::string::find

Look for a literal sequence anywhere in a label before displaying matching stock.

A simple example

std::string::find

  1. Start with "abc", looking for "b"
  2. Do this Search inside the text
  3. Result Found
The middle character matches the text being sought.
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string value = "abc";
    const std::string other = "b";
    std::cout << ((value.find(other) != std::string::npos) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

Syntax

C++ syntax
label.find("tea") != std::string::npos
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Find a word on a shelf label

A stockroom worker searches a shelf description for a particular word. Inspect the match result before using it to highlight the relevant part of the shelf label.

From the programconst std::string label = "green tea bags"
Resultyes
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string label = "green tea bags";
    std::cout << ((label.find("tea") != std::string::npos) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

The letters t-e-a occur consecutively in the label. The check would also match teapot.

Advertisements

Find an ingredient in a lunch order

A search box accepts a short piece of text and looks for it inside a longer description. Check where the sequence occurs before deciding whether the product or shelf label belongs in the results.

From the programconst std::string value = "iced tea"
Resultyes
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string value = "iced tea";
    const std::string other = "tea";
    std::cout << ((value.find(other) != std::string::npos) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

The letters of tea occur together at the end.

Check a filename for a literal dot

A filename filter looks for an actual period between a name and its extension. Treat the period as a literal character so an unrelated filename cannot match merely because it contains some character.

From the programconst std::string value = "readme"
Resultno
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string value = "readme";
    const std::string other = ".";
    std::cout << ((value.find(other) != std::string::npos) ? "yes" : "no") << "\n";
    return 0;
}
Output
no

A dot has no special meaning during a literal search.

Route a lift fault to the right team

A caretaker receives a ticket with the text “East lift stalled on floor 2”. Tickets mentioning a lift go to the lift crew. The incoming system uses lower-case equipment names consistently.

GivenEast lift stalled on floor 2
ResultLift crew
lesson.cpp
#include <string>
#include <iostream>

int main() {
    std::string ticket = "East lift stalled on floor 2";
    std::cout << (ticket.find("lift") != std::string::npos ? "Lift crew" : "General repairs");
    return 0;
}
Output
Lift crew

The word lift occurs inside the ticket text, so the ticket is routed to the lift crew. A search for a literal substring also matches inside longer words.

What to watch for

For whole-word searching, specify word boundaries rather than a plain substring.

Documentation for std::string::find