Library contents

C++ std::string equality

Compare the entire text of two case-sensitive identifiers.

A simple example

std::string equality

  1. Start with "a" and "a"
  2. Do this Compare the whole text
  3. Result Equal
Both inputs contain the same letter in the same case.
lesson.cpp
#include <iostream>
#include <string>

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

Syntax

C++ syntax
value == other
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

Verify a locker code

A reader compares two identifiers that may look similar on their labels. Use the full values under the intended case rules so a difference in one letter or digit is not lost.

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

int main() {
    const std::string value = "B-17";
    const std::string other = "B-17";
    std::cout << ((value == other) ? "yes" : "no") << "\n";
    return 0;
}
Output
yes

Every character in the scan matches the assigned locker.

Advertisements

Distinguish two product variants

A shop sells two variants whose printed product codes differ by letter case. Compare them under the catalog's exact matching policy so the stockroom does not merge separate items.

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

int main() {
    const std::string value = "sku-A";
    const std::string other = "sku-a";
    std::cout << ((value == other) ? "yes" : "no") << "\n";
    return 0;
}
Output
no

The final character has different case.

Validate a locker ticket

The first reading comes from the ticket and the second from the lock. The reader compares both identifiers character for character. Do the two spellings match?

Given'H07' · 'H07'
Worked resultMatch

What to watch for

Unicode text with the same visual appearance may use different normalization forms.

Documentation for std::string equality