<<<Index>>>

Comparison

  • The compare( ) member function returns:

    • 0 if all characters compare equal

    • negative if first char that doesn't match compares to less in the object than in the comparing string

    • positive otherwise.

#include <cassert>
#include <string>
using std::string;
void main()
{
    string s1 = "Hello world";

    // Compare "Hello world" with "Fred":
    int result = s1.compare( "Fred" );

    // Returns positive: H > F
    assert( result > 0 );

    // Compare "world" with "zone":
    result = s1.compare( 6, string::npos, "zone" );

    // Returns negative: w < z
    assert( result < 0 );
}
  • When string::npos specifies the length of the compared substring, all the rest of the string is assumed.

<<<Index>>>