C++20 sections · 1024 units
Open in Course

Comparing Strings

Testing string relationships

Compare strings with ==, !=, <, >, <=, >=. These work intuitively: s1 == s2 checks identical characters. Comparison is case-sensitive: "Hello" and "hello" differ. Inequality operators use lexicographic order (dictionary order).

"apple" < "banana" is true because 'a' comes before 'b'. Comparison proceeds character by character until difference. These operators compare content, not addresses. Two different variables containing "hello" are equal.

This differs from C-style strings where == compares pointers.