zoukankan      html  css  js  c++  java
  • std::string::compare() in C++

    Syntax 1: Compares the string *this with the string str.
    int string::compare (const string& str) const
    Returns:
    0 : if both strings are equal.
    A value < 0 : if *this is shorter than str or,
    first character that doesn't match is smaller than str.
    A value > 0 : if *this is longer than str or,
    first character that doesn't match is greater
    // CPP code for demonstrating
    // string::compare (const string& str) const
    
    #include<iostream>
    using namespace std;
    
    void compareOperation(string s1, string s2)
    {
        // returns a value < 0 (s1 is smaller then s2)
        if((s1.compare(s2)) < 0)
            cout << s1 << " is smaller than " << s2 << endl;
    
        // returns 0(s1, is being comapared to itself)
        if((s1.compare(s1)) == 0)
            cout << s1 << " is equal to " << s1 << endl;
        else
            cout << "Strings didn't match ";
        
    }
    
    // Driver Code
    int main()
    {
        string s1("Geeks");
        string s2("forGeeks");
        compareOperation(s1, s2);
        
        return 0;
    }
    View Code

    output:

    Geeks is smaller than forGeeks
    Geeks is equal to Geeks

    Syntax 2: Compares at most, len characters of string *this, starting with index idx with the string str.

    // CPP code to demonstrate
    // int string::compare (size_type idx, size_type len,
    // const string& str) const
    
    #include<iostream>
    using namespace std;
    
    void compareOperation(string s1, string s2)
    {
        // Compares 5 characters from index number 3 of s2 with s1
        if((s2.compare(3, 5, s1)) == 0)
            cout << "Here, "<< s1 << " are " << s2;
    
        else
            cout << "Strings didn't match ";
    }
    // Driver Code
    int main()
    {
        string s1("Geeks");
        string s2("forGeeks");
        compareOperation(s1, s2);
        
    return 0;
    }
    View Code

    output:

    Here, Geeks are forGeeks

    Syntax 3: Compares at most, len characters of string *this starting with index idx with at most, str_len characters of string str starting with index str_idx.

    int string::compare (size_type idx, size_type len, const string& 
    str, size_type str_idx, size_type str_len) const
    Throws out_of_range if idx > size().
    Throws out_of_range if str_idx > str.size().
    // CPP code to demonstrate
    // int string::compare (size_type idx, size_type len, const string&
    // str, size_type str_idx, size_type str_len) const
    
    #include<iostream>
    using namespace std;
    
    void compareOperation(string s1, string s2)
    {
        // Compares 5 characters from index number 0 of s1 with
        // 5 characters from index 3 of s2
        if((s1.compare(0, 5, s2, 3, 5)) == 0)
            cout << "Welcome to " << s1 << s2 << " World";
    
        else
            cout << "Strings didn't match ";
    }
    // Driver Code
    int main()
    {
        string s1("Geeks");
        string s2("forGeeks");
        compareOperation(s1, s2);
        
    return 0;
    }

    Output:

    Welcome, to GeeksforGeeks World
  • 相关阅读:
    WinForm RadioButton 默认不选中
    使用Devexpress时,ArcEngine 加载地图空白。
    正尝试在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。
    Vue使用iframe加载本地html,并进行通信,传递参数
    找不到 Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager 服务的实例
    计算经纬度两点间距离
    The system clock has been set back more than 24 hours
    nmcli命令行修改网络连接名称
    linux安装mysql5.7
    <<<物品借记登记>>>
  • 原文地址:https://www.cnblogs.com/PiaYie/p/14605653.html
Copyright © 2011-2022 走看看