[TOC]
String
头文件:include <string>
使用空间:using namesapce std;
创建字符串
- 复制拷贝
//直接全部复制
string str("01234");
//从下标0开始复制3偏移量
string str("012345",3);
//从下标1开始复制3偏移量
string str("012345",1,3);
- 用字符构造
//用3个字符a构造
string str(3,'a');
- “=”直接复制拷贝
string str = "str";
访问元素方式,存取和修改
[],at
// [],at() :元素存取
string stringAt = "trianglestring";
cout << "stringAt[3] = " <<stringAt[3] << endl;
cout << "stringAt.at(3) = " << stringAt.at(3) << endl;
stringAt[3] = '6';
stringAt.at(5) = '9';
cout << "stringAt = " << stringAt << endl;
添加或者拼接字符/字符串
+ 、 append
string stringOrigin = "chen";
string stringAppend = "yufeng";
stringOrigin = stringOrigin + stringAppend;
cout << "stringOrigin = " << stringOrigin << endl;
stringOrigin.append("_OK");
cout << "stringOriginAppend = " << stringOrigin << endl;
在指定位置插入字符串
insert
// insert:在指定position插入字符串
string stringInsertOrigin = "trianglestring";
stringInsertOrigin.insert(3, "__");
cout << "stringInsertOrigin = " << stringInsertOrigin << endl;
替换字符串
replacea
// replace: 替换字符串,某个pos位置开始的size个字符替换成后面的“”字符串
string stringReplaceOrigin = "trianglestring";
stringReplaceOrigin.replace(3, 2, "66");
cout << "stringReplaceOrigin = " << stringReplaceOrigin << endl;
取子字符串
substr
// substr
string stringSubstr = "trianglestring";
// 从索引为4开始的3个字符
cout << "stringSubstr.substr(4,3) = " << stringSubstr.substr(4,3) << endl;
// 从索引为4开始的所有字符
cout << "stringSubstr.substr(4) = " <<stringSubstr.substr(4) << endl;
// 整个字符
cout << "stringSubstr.substr() = " <<stringSubstr.substr() << endl;
查找字符/字符串
find,find_first_of,find_last_of
// find
string stringFind = "trianglestring";
stringFind.find('n');
cout << "stringFind.find('n') = " << stringFind.find('n') << endl;
cout << "stringFind.find_first_of('e') = " << stringFind.find_first_of('e') << endl;
cout << "stringFind.find_last_of('e') = " << stringFind.find_last_of('e') << endl;
交换两个字符串
swap
string str1 = "chen";
string str2 = "yufeng";
swap(str1, str2);
比较字符串:从开始的第一个字符比较,长度是次要的
== 、!= 、 > 、<
compare
// ==,<,>,<=,>=:比较字符串
string stringLeft = "zhen";
string stringRight = "yufeng";
if (stringLeft == stringRight)
{
cout << "equal" << endl;
}
if (stringLeft != stringRight)
{
cout << "not equal" << endl;
}
if (stringLeft < stringRight)
{
cout << "stringLeft < stringRight" << endl;
}
if (stringLeft > stringRight)
{
cout << "stringLeft > stringRight" << endl;
}
// compare()
string stringCompare = "trianglestring";
int aaa = stringCompare.compare("chen"); // > 0
int bbb = stringCompare.compare("trianglestring"); // == 0
int ccc = stringCompare.compare("done"); // < 0
cout << "aaa = " << aaa << ";bbb = " << bbb << ";ccc = " << ccc << endl;
删除字符串或清空字符串
erase 指定删除起始到偏移的字符
clear 清空字符串
// erase: 删除字符
string stringEraseOrigin = "trianglestring";
stringEraseOrigin.erase(2,1);
cout << "stringEraseOrigin = " << stringEraseOrigin << endl;
// clear :删除全部字符
string stringClearOrigin = "trianglestring";
stringClearOrigin.clear();
cout << "stringClearOrigin = " << stringClearOrigin << endl;
字符串长度
size,length 没有区别
empty 判空
// size(), length():计算字符串长度
string stringCount = "trianglestring";
cout << "stringSize = " << stringCount.size() << endl;
cout << "stringLength = " << stringCount.length() << endl;
// empty():判断字符串是否为空
string stringIsEmpty = "";
string stringNotEmpty = "chen";
if (stringIsEmpty.empty())
{
cout << "stringIsEmpty == empty" << endl;
}
else
{
cout << "stringIsEmpty != empty" << endl;
}
if (stringNotEmpty.empty())
{
cout << "stringNotEmpty == empty" << endl;
}
else
{
cout << "stringNotEmpty != empty" << endl;
}
最大容量
max_size
// max_size: 最大能表示的字符串长度
string stringMaxSize;
cout << "stringMaxSize = " << stringMaxSize.max_size() << endl;
网友评论