C ++For numeric data, you can use "=" to compare whether the values are equal, but it is not applicable to string comparison. We can use the following two functionsCompare whether two strings are equal.
1,Strcmp
This isUsed For ANSI standard stringsFunction (such as string and char *), which accepts two string buffers as parameters,Returns zero if the two strings are the same.. Otherwise, if the value of the first input string is greater than the return value of the second string, it is greater than zero. If the value of the first input string is smaller than the return value of the second string, it is less than zero.
Char * Ch = "Xiangxiang sugar ";
If (Strcmp(CH, "Xiangxiang sugar") = 0)
{
//Equal strings
}
Else
{
//Strings are not equal
}
2,Wcscmp
This function is a unicode Series function corresponding to strcmp. It is used in the same way as strcmp.Compare whether two Unicode strings are equal(Such as wstring and wchar_t *).
Wchar_t * Ch = l "Xiangxiang sugar ";
If (Wcscmp(CH, l "Xiangxiang sugar") = 0)
{
//Equal strings
}
Else
{
//Strings are not equal
}
Note: The comparison functions described above are case-sensitive for English. Even if the letters are the same but the cases are different, the functions will regard the two strings as different.
We have discussed how to compare the two strings to determine whether they are equal, Which is case sensitive. Today, we also introduce two functions that determine whether a string is equal. The difference is thatThey are case insensitive.That is, the function does not distinguish whether the strings are in the same case, as long as each letter is the same.
1,Stricmp
This isUsed For ANSI standard stringsFunction (such as string and char *), which accepts two string buffers as parameters,If the two strings are the same, zero is returned, Which is case insensitive.. Otherwise, if the value of the first input string is greater than the return value of the second string, it is greater than zero. If the value of the first input string is smaller than the return value of the second string, it is less than zero.
Char * Ch = "ABCD ";
If (Stricmp(CH, "ABCD") = 0)
{
//Equal strings
}
Else
{
//Strings are not equal
}
2,Wcsicmp
This function is a unicode Series function corresponding to stricmp. It is used in the same way as stricmp.Checks whether two Unicode strings are equal and case insensitive.(Such as wstring and wchar_t *).
Wchar_t * Ch = l "ABCD ";
If (Wcsicmp(CH, l "ABCD") = 0)
{
//Equal strings
}
Else
{
//Strings are not equal
}
Strcmp wcscmp stricmp wcsicmp ()