logo

std::string::compare() in C++

vergelijken() is een openbare lidfunctie van de stringklasse. Het vergelijkt de waarde van het stringobject (of een substring) met de reeks tekens die door de argumenten wordt gespecificeerd.
De Compare() kan meer dan één argument voor elke string verwerken, zodat men een substring kan specificeren op basis van de index en de lengte ervan.
Retourtype: Compare() retourneert een geheel getal in plaats van een Booleaanse waarde.
Verschillende syntaxis voor string::compare() :

    Syntaxis 1: Vergelijkt de string *this met de 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: als *dit langer is dan str of, is het eerste teken dat niet overeenkomt groter>

CPP






// CPP code for demonstrating> // string::compare (const string& str) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> >// returns a value <0 (s1 is smaller than s2)> >if>((s1.compare(s2)) <0)> >cout << s1 << ' is smaller than ' << s2 << endl;> >// returns 0(s1, is being compared 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;> }>

>

>

Uitgang:

Geeks is smaller than forGeeks Geeks is equal to Geeks>
    Syntaxis 2: Vergelijkt maximaal len-tekens van string *this, beginnend met index idx met de string str.
 int string::compare (size_type idx, size_type len, const string& str) const Throws out_of_range if index>grootte().>

CPP


een datum formatteren in Java



// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const string& str) const> #include> 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;> }>

>

>

Uitgang:

Here, Geeks are forGeeks>
    Syntaxis 3: Vergelijkt maximaal len-tekens van string *dit begint met index idx met maximaal str_len-tekens van string str beginnend met 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>maat(). Genereert out_of_range als str_idx> str.size().>

CPP




commando alle hoofdletters excel

// 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> 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;> }>

>

>

Uitgang:

Welcome, to techcodeview.com World>
    Syntaxis 4: Vergelijkt de karakters van string *this met de karakters van de C-string cstr.
 int string::compare (const char* cstr) const>

CPP


java cast string naar int



// CPP code to demonstrate> // int string::compare (const char* cstr) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> >// returns <0 (s1 < 'techcodeview.com')> >if>((s1.compare('techcodeview.com')) <0)> >cout << s1 << ' is smaller than string ' << 'techcodeview.com';> >//returns 0 (s2 is 'forgeeks')> >if>((s2.compare('forGeeks')) == 0)> >cout << endl << s2 << ' is equal to string ' << s2;> >else> >cout << 'Strings didn't match ';> > }> // Driver Code> int> main()> {> >string s1('Geeks');> >string s2('forGeeks');> >compareOperation(s1, s2);> > >return> 0;> }>

>

>

Uitgang:

Geeks is smaller than string techcodeview.com forGeeks is equal to string forGeeks>
    Syntaxis 5: Vergelijkt maximaal len-tekens van string *this, beginnend met index idx met alle tekens van de C-string cstr.
 int string::compare (size_type idx, size_type len, const char* cstr) const>

Houd er rekening mee dat cstr mogelijk geen nulaanwijzer (NULL) is.

CPP




// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const char* cstr) const> #include> using> namespace> std;> void> compareOperation(string s1)> {> >// Compares 5 characters from 0 index of s1 with 'Geeks'> >if>((s1.compare(0, 5, 'Geeks')) == 0)> >cout << s1 << ' are ' << 'awesome people';> > >else> >cout << 'Strings didn't match ';> > }> // Driver Code> int> main()> {> >string s1('Geeks');> >compareOperation(s1);> > >return> 0;> }>

>

>

Uitgang:

Geeks are awesome people>
    Syntaxis 6: Vergelijkt maximaal len-tekens van string *this, beginnend met index idx met chars_len-tekens van de karakterarray-chars.
 int string::compare (size_type idx, size_type len, const char* chars,  size_type chars_len)const>

Houd er rekening mee dat tekens ten minste chars_len-tekens moeten bevatten. De tekens kunnen willekeurige waarden hebben. ‘ ’ heeft dus geen speciale betekenis.

CPP




// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const char* chars, size_type chars_len)const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> >// Compares 5 characters from 0 index of s1 with> >// 5 characters of string 'Geeks'> >if>((s1.compare(0, 5, 'Geeks', 5)) == 0)> >cout << 'This is ' << s1 << s2 ;> > >else> >cout << 'Strings didn't match ';> }> // Driver Code> int> main()> {> >string s1('Geeks');> >string s2('forGeeks');> >compareOperation(s1, s2);> > >return> 0;> }>

>

hiba bukhari

>