如何在C++中使用String compare()?
时间:2020-02-23 14:30:05  来源:igfitidea点击:
在本文中,我们将重点介绍C++字符串compare()函数及其变体的工作。
C++中的String compare()入门
" C++字符串库"包含用于处理字符串数据和操作字符的大量函数。
C++中的string.compare()以字典方式比较字符串值,即比较可比较字符串的字符的ASCII值。
语法:
string1.compare(string2)
如果字符串完全相同,则compare()函数返回0。
并且,如果字符串不相等,该函数将返回以下值之一:
- "值<0":如果string1的第一个不匹配字符的ASCII值小于string2的第一个字符的ASCII值,则该函数返回小于0的值。 
- "值> 0":如果string1的第一个不匹配字符的ASCII值大于string2的第一个字符的ASCII值,则该函数返回大于0的值。 
例:
#include<iostream> 
using namespace std; 
int main() 
{ 
	string str1="Python"; 
	string str2="Python with theitroad"; 
	if((str1.compare(str2))==0)
	    cout<<"Strings are equal!!";
	else
	    cout<<"Strings are not equal!!";
	return 0; 
} 
输出:
Strings are not equal!!
C++中的字符串compare()的变体
C++ String compare()函数可以以不同的形式用于相应地比较字符串。
让我们在以下部分中了解string.compare()函数的不同变体。
变体1:使用索引将字符串的一部分与其他字符串进行比较
在此变体中,我们可以使用以下语法将string1的一部分与string2进行比较:
语法:
string1.compare(start-index, end-length, string2)
- start-index:要与string2比较的string1的起始索引。 
- " end-length":字符串1的长度,字符串1的部分需要与字符串2进行比较。 
例:
#include<iostream> 
using namespace std; 
int main() 
{ 
	string str1="Python"; 
	string str2="Python with theitroad"; 
	if((str2.compare(0, 6, str1))==0)
	    cout<<"Python with theitroad contains Python";
	else
	    cout<<"Python with theitroad does not contain Python";
	return 0; 
} 
在以上示例中,将str2的字符从索引0到长度6的部分(即" Python")与str1进行了比较。
输出:
Python with theitroad contains Python
方案2:将字符串与C字符串的字符进行比较
使用string.compare()函数可以使用以下语法将字符串与C字符串格式的字符进行比较:
语法:
string.compare(characters)
例:
#include<iostream> 
using namespace std; 
int main() 
{ 
	string str1="Python"; 
	string str2="Python with theitroad"; 
	if((str1.compare("Python")) == 0)
	    cout<<"Strings are equal!\n";
	else
	    cout<<"Strings are not equal!";
	
	if((str2.compare("theitroad")) > 0)
	    cout<<"Python with theitroad is greater than theitroad";
	else
	    cout<<"Python with theitroad is less than theitroad";
	
	return 0; 
} 
输出:
Strings are equal! Python with theitroad is greater than theitroad
方案3:使用string.compare()函数比较字符串的一部分
string.compare()函数可以帮助我们使用索引比较字符串的各个部分。
语法:
string1.compare(start-index, end-length, string2, start-index, end-length)
例:
#include<iostream> 
using namespace std;
int main() 
{ 
	string str1="Python"; 
	string str2="Python with theitroad"; 
	if((str1.compare(0, 6, str2, 0, 6)==0))
	    cout<<"Strings are equal!\n";
	else
          cout<<"Strings are not equal!";
	
	return 0; 
} 
输出:
Strings are equal!
变体4:比较字符串的一部分和C字符串
通过使用以下语法指定索引,我们可以将字符串的某些部分与C字符串进行比较:
语法:
string.compare(start-index, end-length,'C-string')
例:
#include<iostream> 
using namespace std; 
int main() 
{ 
	string str1="Python"; 
	//string str2="Python with theitroad"; 
	if((str1.compare(0, 6, "Python") == 0))
	    cout<<"Strings are equal!\n";
	else
	    cout<<"Strings are not equal!";
	
	return 0; 
} 
输出:
Strings are equal!
变体5:使用compare()函数将字符串的部分(字符)与C字符串的一部分进行比较
语法:
string.compare(start-index, end-length,'C-string',end-length)
例:
#include<iostream> 
using namespace std; 
int main() 
{ 
	string str1="Python"; 
	//string str2="Python with theitroad"; 
	if((str1.compare(0, 6, "Python",6) == 0))
	    cout<<"Strings are equal!\n";
	else
	    cout<<"Strings are not equal!";
	
	return 0; 
} 
输出:
Strings are equal!

