C++中的字符串有两种形式:
(1)特殊的字符数组,以‘\0’标记字符数组的结束。
(2)string类型的简单变量形式。赋值和拼接可以直接使用“=”“+”等运算符。
整体基于下面这三个字符串来实验。(本身是一个程序,拆分来说明字符串功能函数)
string str1;
string str2 = "abcdefg";
string str3 = "ABCDEFG";//字符串的赋值
①字符串的复制
//字符串的复制
str1.assign(str2, 0, 4);
cout << str1 << endl;
str1.assign(str2);
cout << str1 << endl;
str1 = str2;
cout << str1 << endl << endl;
输出结果检验:
abcd
abcdefg
abcdefg
②求字符串长度
//字符串长度
cout << str1.size() << endl << str1.length() << endl;
输出结果检验:
7
7
③交换两个字符串内容
//交换两个字符串内容
str2.swap(str3);
cout << str2 << endl << str3<<endl;
输出结果检验:
ABCDEFG
abcdefg
④
在字符串末尾插入字符
在指定位置插入字符
str1: abcdefg
str2: ABCDEFG
str3: abcdefg
//在字符串末尾插入字符
str1 += 'h';
cout << str1 << endl;
str3.append(str1,7,1);
cout << str3 << endl;
str1.push_back('i');
cout << str1 << endl;
//这个函数,作用是字符串之后插入一个字符。
//注:VC6.0中未通过,string中无push_back的定义。
cout << endl;
//在字符串指定位置插入字符串
str1.insert(1, "b");
cout << str1 << endl;
str1.insert(0, str2);
cout << str1 << endl;
cout << endl;
输出结果检验:
abcdefgh
abcdefgh
abcdefghi
abbcdefghi
ABCDEFGabbcdefghi
删除指定位置的字符
清空字符串以及判断字符串是否为空
str1: ABCDEFGabbcdefghi
str2: ABCDEFG
str3: abcdefgh
//删除规定位置的字符
//int len2 = str2.length();
//cout <<len2 <<endl;//7
str1.erase(0, str2.length());
cout << str1 << endl;
str1.erase(1, 1);
cout << str1 << endl;
cout << endl;
输出结果检验:
abbcdefghi
abcdefghi
⑤清空字符串以及判断字符串是否为空
str1: abcdefghi
在str1.clear();
if (str1.empty())
cout << " 是空串" << endl;
else
cout << "不是空串" << endl;
输出结果检验:
是空串
⑥字符串的替换和字符串的拼接
str1: ABCDEFG
str2: abcdefgh
str3: abcdefgh
str1.replace(0,str3.length(),str3);
cout << str1 << endl;
//str1的0-str3.size长度处被替换为str3,str1长度小于str3,即用str3覆盖str1
str1.replace(0, str1.size(), str3, 4);
cout << str1 << endl;
//str1整体被替换为str3的第五个元素开始之后的内容
str1.replace(0, str1.size(), str3, 0,4);
cout << str1 << endl;
//str1整体被替换为str3的前四个元素的内容
str1.replace(0, 2, " ");
cout << str1 << endl;
//前两个元素被替换为一个空格
str1 = str3;
cout << str1 << endl;
str1.replace(0, 5, str2, 3, 3);
cout << str1 << endl;
//str1的前五个元素被替换为str2字符串第4个元素开始的三个元素
//字符串拼接
str2 += str3;
cout << str2 << endl;
输出结果检验:
abcdefgh
efgh
abcd
cd
abcdefgh
DEFfgh
ABCDEFGabcdefgh
⑦字符串的比较
str1: DEFfgh
str2: ABCDEFGabcdefgh
str3: abcdefgh
//字符串比较 == != < > <= >
cout << endl;
cout << str1 << endl << str2 << endl << str3 << endl;
if (str2.compare(str3) < 0)
cout << "str2<str3" << endl;
else
if (str2.compare(str3) > 0)
cout << "str2>str3" << endl;
else
cout << "str2=str3" << endl;
//ut << str2.compare(str3)<<endl;//<0说明str2<str3
cout << str3.compare(0, 3, str2) << endl;
cout << str2.compare(9,3,str3, 0, 3)<<endl;//输出1为逻辑1
cout << endl;
输出结果检验:
str2<str3
1
1
⑧
存取字符at()函数
提取字符substr()函数
//存取某个字符
cout << str1 << endl;
cout << str1.at(4) << endl;
//cout << str1[4] << endl;
str1.at(4) = 'G';
cout << str1 << endl;
cout << str3 << endl;
//提取子串substr
string str4 = "1234";
str3 += str4.substr(0, 2);
cout << str3 << endl;
输出结果检验:
DEFfgh
g
DEFfGh
abcdefgh
abcdefgh12
⑨C++语言中string和char数组的输入
string name;
getline(cin, name);//string型变量输入
cout << name << endl;
const int Size = 20;
char name[Size];
cin.getline(name, Size);
cout << name << endl;