#include<string.h>
#include<iostream>
using namespace std;
int main() {
char a[50] = "hello";
char b[50];
int aLength = strlen(a);//求字符串长度
cout << "aLength=" << aLength << endl;
strcpy(b, a);//将字符数组a拷贝给b
cout << "b=" << b << endl;
strcat(a,b);//将b连接到a
cout << "strcat result=" << a << endl;
int cmp = strcmp(a, b);//从字符数组开头开始,依次比较每个字符,直到出现差异、或者其中一个串结束为止。
cout << "strcmp result=" << cmp << endl;
}
#include<string>
#include <iostream>
using namespace std;
int main()
{
string str1 = "Hello"; //初始化string类型,并具体赋值
string str2 = " world!";
int str1Length = str1.length();
cout << "str1Length=" << str1Length << endl;
string str3 = str1 + str2;
cout << "add result="<<str3 << endl;
int cmp = str1 > str2 ? 1 : str1==str2 ? 0:-1; //从字符串左边开始,依次比较每个字符,直到出现差异、或者其中一个串结束为止。
cout << "compare result=" << cmp << endl;
string str4 = str1.substr(2);
cout << "substr(2)=" << str4 << endl;
string str5 = str1.substr(1, 3);
cout << "substr(1,3)=" << str5 << endl;//ell
int index = str1.find('l', 0);
cout << "find index=" << index << endl;
}