C风格字符串 基本示例
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main()
{
char a1 [20] {"lbw"};
char a2 [20] {};
strcpy(a2, a1);
cout << "a2 :" << a2 << endl;
strcat(a1, "_");
strcat(a1, a2);
cout << "拼接结果:" << a1 << endl;
return 0;
}
char数组 查看字符串的长度
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a1[]{ "abcdefg"};
int len1;
len1 = strlen(a1);
cout << len1 << endl;
return 0;
}
C++ 字符串基本使用
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
string s2 {"hello"};
string s3 {s2};
string s4 {"hello", 4};
string s5 {s2, 1, 3};
string s6 {5, 'x'};
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
cout << "s5:" << s5 << endl;
cout << "s6:" << s6 << endl;
string n1;
n1 = "奥里给";
cout << "n1: " << n1 << endl;
string n2 {"NO NO NO"};
n1 = n2;
cout << "n1: " << n1 << endl;
string f1 {"C++ = "};
string f2 {"无敌"};
string f3;
f3 = f1 + f2;
cout << "f3: " << f3<< endl;
return 0;
}
指针访问字符串
string str_1 = "ab12";
string* s = &str_1;
cout << *s << endl;