Comparing strings - C++ STL

C++ examples for STL:string

Description

Comparing strings

Demo Code

#include <iostream>
#include <string>

int main(int argc, const char* argv[]) {
    std::string string1("Testing the comparision functions.");
    std::string string2("Hello");
    std::string string3("stinger");
    std::string string4(string2);/*from w  w w.j  av a 2  s  .  c  o  m*/

    std::cout << "string1: " << string1 << "\nstring2: " << string2
              << "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n";

    // comparing string1 and string4
    if (string1 == string4)
        std::cout << "string1 == string4\n";
    else {
        if (string1 > string4)
            std::cout << "string1 > string4\n";
        else
            std::cout << "string1 < string4\n";
    }

    // comparing string1 and string2
    int result = string1.compare(string2);

    if (result == 0)
        std::cout << "string1.compare(string2) == 0\n";
    else {
        if (result > 0)
            std::cout << "string1.compare(string2) > 0\n";
        else
            std::cout << "string1.compare(string2) < 0\n";
    }

    // comparing string1 (elements 2-5) and string3 (elements 0-5)
    result = string1.compare(2, 5, string3, 0, 5);

    if (result == 0)
        std::cout << "string1.compare(2, 5, string3, 0, 5) == 0\n";
    else {
        if (result > 0)
            std::cout << "string1.compare(2, 5, string3, 0, 5) > 0\n";
        else
            std::cout << "string1.compare(2, 5, string3, 0, 5) < 0\n";
    }

    // comparing string2 and string4
    result = string4.compare(0, string2.length(), string2);

    if (result == 0)
        std::cout << "string4.compare(0, string2.length(), string2) == 0"
                  << std::endl;
    else {
        if (result > 0)
            std::cout << "string4.compare(0, string2.length(), string2) > 0"
                      << std::endl;
        else
            std::cout << "string4.compare(0, string2.length(), string2) < 0"
                      << std::endl;
    }

    // comparing string2 and string4
    result = string2.compare(0, 3, string4);

    if (result == 0)
        std::cout << "string2.compare(0, 3, string4) == 0" << std::endl;
    else {
        if (result > 0)
            std::cout << "string2.compare(0, 3, string4) > 0" << std::endl;
        else
            std::cout << "string2.compare(0, 3, string4) < 0" << std::endl;
    }

    return 0;
}

Result


Related Tutorials