Library contents

C++ std::string::compare

Check the beginning of a reference before routing it to the billing queue.

A simple example

std::string::compare

  1. Start with "abc" and "ab"
  2. Do this Compare the beginning
  3. Result Yes
The first two characters match the requested prefix.
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string value = "abc";
    const std::string other = "ab";
    std::cout << ((value.compare(0, other.size(), other) == 0) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

Syntax

C++ syntax
label.compare(0, 4, "INV-") == 0
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

Recognize an invoice prefix

A finance inbox recognizes invoices by a specific prefix on their reference codes. Inspect the beginning of the code before placing an incoming record in the invoice queue.

From the programconst std::string label = "INV-482"
Resultyes
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string label = "INV-482";
    std::cout << ((label.compare(0, 4, "INV-") == 0) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

The first four characters are INV-, so this order enters the invoice queue.

Advertisements

Route a finance request

An intake queue uses a recognizable prefix or status to route incoming work. Check the required position and comparison rules so a matching fragment in an unrelated field cannot send a request to the wrong desk.

From the programconst std::string value = "FIN-482"
Resultyes
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string value = "FIN-482";
    const std::string other = "FIN-";
    std::cout << ((value.compare(0, other.size(), other) == 0) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

The prefix identifies the finance queue.

Reject a prefix in the middle

A test label contains a routing token later in its text rather than at the beginning. Confirm that the prefix check rejects it so a keyword inside an unrelated label cannot trigger routing.

From the programconst std::string value = "OLD-FIN-482"
Resultno
lesson.cpp
#include <iostream>
#include <string>

int main() {
    const std::string value = "OLD-FIN-482";
    const std::string other = "FIN-";
    std::cout << ((value.compare(0, other.size(), other) == 0) ? "yes" : "no") << "\n";
    return 0;
}
Output
no

The expected text occurs later, so this is a different kind of label.

What to watch for

Prefix matching here is case-sensitive. Keep the full identifier when handing it to the next step.

Documentation for std::string::compare