#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "hello";
string s2 = "aello";
int ret = s1.compare(s2);
if (ret == 0)
{
cout << "s1 == s2" << endl;
}
else if (ret > 0)
{
cout << "s1 > s2" << endl;
}
else
{
cout << "s1 < s2 " << endl;
}
}
int main()
{
test01();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void test0()
{
string str = "abcdefg";
string subStr = str.substr(1, 3);
cout << "sunStr = " << subStr << endl;
string email = "hello@sina.com";
int pos = email.find("@");
string username = str.substr(0, pos);
cout << "username: " << username << endl;
}
int main()
{
test0();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str = "Hello World";
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
str[0] = 'x';
str.at(2) = 'o';
cout << str << endl;
}
int main()
{
test01();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str = "hello";
str.insert(1, "111");
cout << str << endl;
str.erase(1, 3);
cout << str << endl;
}
int main()
{
test01();
return 0;
}