1.string值的比较
#include<iostream>
using namespace std;
int main()
{
string str1("hello");
string str2("world");
if(str1 > str2)
{
cout << "str1 > str2" << endl;
}
else if (str < str2)
{
cout << "str1 < str" << endl;
}
else
{
cout << "str1 == str2" << endl;
}
char str3[20] = "I love you ";
if(str3 < str1)
{
cout << "str3 < str" << endl;
}
}
2.string值的定位
#include<iostream>
using namespace std;
int main()
{
string str1("hello");
string str2("world");
cout << string::npos << endl;
cout << "tr1.find(\"l\") = " << str1.find("l") << endl;
cout << "tr1.find(\"l\", 3) = " << str1.find("l", 3) << endl;
if(str1.find("l", 4) == string::npos)
{
cout << "未找到相应的字符" << endl;
}
cout << "str1.find(\"l\", 4) = " << str1.find("l", 4) << endl;
cout << "str1.rfind(\"l\") = " << str1.rfind("l", 4) << endl;
cout << "str2.find_first_of(\"hero\") = " << str2.find_first_of("hero") << endl;
cout << "str2.find_last_of(\"hero\") = " << str2.find_last_of("hero") << endl;
cout << "str2.find_first_not_of(\"hero\") = " << str2.find_first_not_of("hero") << endl;
return 0;
}