The string::compare()
function in C++ provides a powerful way to compare strings and substrings. This function, part of the <string>
header file, allows you to determine if two strings are equal, or if one is lexicographically greater or less than the other. Understanding how to leverage string::compare()
effectively is crucial for various string manipulation tasks. This article will guide you through its usage with clear examples.
Comparing Two Strings Directly
The simplest use case involves comparing two entire strings. The function returns an integer indicating the comparison result.
Syntax
str1.compare(str2);
Parameters
str1
: The first string to compare.str2
: The second string to compare.
Return Value
0
: ifstr1
is equal tostr2
.- A positive integer: if
str1
is lexicographically greater thanstr2
. - A negative integer: if
str1
is lexicographically less thanstr2
.
Example
#include <iostream>
#include <string>
int main() {
std::string str1("Geeks");
std::string str2("Geeks");
if (str1.compare(str2) == 0) {
std::cout << "Strings are equal" << std::endl;
} else {
std::cout << "Strings are not equal" << std::endl;
}
return 0;
}
Comparing a Substring with Another String
string::compare()
allows you to compare a specific portion (substring) of a string with another string.
Syntax
str1.compare(pos, len, str2);
Parameters
str1
: The string containing the substring.pos
: The starting position of the substring instr1
.len
: The length of the substring.str2
: The string to compare the substring with.
Return Value
Similar to the previous case, the return value indicates the lexicographical comparison result between the substring and str2
.
Example
#include <iostream>
#include <string>
int main() {
std::string str1("ProgrammingGeeks");
std::string str2("Geeks");
if (str1.compare(11, 5, str2) == 0) {
std::cout << "Substring matches" << std::endl;
} else {
std::cout << "Substring does not match" << std::endl;
}
return 0;
}
Comparing Two Substrings
You can also compare specific portions of two different strings.
Syntax
str1.compare(pos1, len1, str2, pos2, len2);
Parameters
str1
: The first string.pos1
: The starting position of the first substring.len1
: The length of the first substring.str2
: The second string.pos2
: The starting position of the second substring.len2
: The length of the second substring.
Return Value
The return value signifies the lexicographical comparison result between the two substrings.
Example
#include <iostream>
#include <string>
int main() {
std::string str1("LearnCppProgramming");
std::string str2("CppForBeginners");
if (str1.compare(5, 3, str2, 0, 3) == 0) {
std::cout << "Substrings match" << std::endl;
} else {
std::cout << "Substrings do not match" << std::endl;
}
return 0;
}
Conclusion
The string::compare()
function in C++ offers versatile ways to perform string comparisons, whether it’s comparing entire strings or specific substrings. By understanding its different forms and return values, you can effectively utilize this function for accurate string manipulation and analysis in your C++ programs. Remember to consult the C++ documentation for a comprehensive understanding of all the available options and edge cases.