String类函数简讲

1.构造:

b) string s(str) //拷贝构造函数 生成str的复制品

c) string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值
d) string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
g) string s(num,c) //生成一个字符串,包含num个c字符


2.字符串操作函数

b) swap()     //交换两个字符串的内容

d) insert() //插入字符

这种形式的insert()函数不支持传入单个字符,这时的单个字符必须写成字符串形式

string类的插入函数: 
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符


e) erase() //删除字符

string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串


f) clear() //删除全部字符

g) replace() //替换字符

string类的替换函数: 
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s


i) ==,!=,<,<=,>,>=,compare()    //比较字符串

compare()。他支持多参数处理,支持用索引值和长度定位子串来进行比较。他返回一个整数来表示比较结果,返回值意义如下:0 - 相等      >0-大于     <0-小于。举例如下:
string s(“abcd”);
s.compare(“abcd”); //返回0
s.compare(“dcba”); //返回一个小于0的值
s.compare(“ab”); //返回大于0的值
s.compare(s); //相等
s.compare(0,2,s,2,2); //用”ab”和”cd”进行比较 小于零
s.compare(1,2,”bcx”,2); //用”bc”和”bc”比较


j) size(),length()    //返回字符数量
l) empty()    //判断字符串是否为空
p) >>,getline() //从stream读取某值

u) substr() //返回某个子字符串

提取子串和字符串连接
题取子串的函数是:substr(),形式如下:
s.substr();//返回s的全部内容
s.substr(11);//从索引11往后的子串
s.substr(5,6);//从索引5开始6个字符


v)查找函数

string类的查找函数: 
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;


w)begin() end() //提供类似STL的迭代器支持

x)transform(str.begin(),str.end(),str.begin(),::tolower);//转小写

transform(s.begin(), s.end(), s.begin(), ::toupper);//转大写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值