string函数

#include <string.h>
#include <iostream>
using namespace std;
int main()
{
	string ch1="aabc";
	string ch2="123";
	cout<<ch1+ch2<<endl;
	
	string ch3(10,'s');//生成10个s 
	cout<<ch3;
	cout<<endl;
	
	ch1.insert(1,ch2);//在下标1处插入ch2 
	cout<<ch1<<endl;
	
	ch1.replace(0,2,ch2);//在下标0~(2-1)区间换成ch2 
	cout<<ch1<<endl;
	
	cout<<ch1.find('c')<<endl;//找字符输出下标 
	
	ch1.erase(0,1);//删除0~1-1位置的字符 
	cout<<ch1<<endl;
	return 0;
}
string类的构造函数如下:
a) string s; /// 生成一个空字符串s 
b) string s( str ) /// 拷贝构造函数,生成str的复制品 
c) string s( str, stridx ) /// 将字符串str内"始于位置stridx"的部分当作字符串的初值 
d) string s( str, stridx, strlen ) /// 将字符串str内"始于stridx且长度顶多strlen"的部分作为字符串的初值 
e) string s( cstr ) /// 将C字符串作为s的初值 
f) string s( chars, chars_len ) /// 将C字符串前chars_len个字符作为字符串s的初值
g) string s( num, c ) /// 生成一个字符串,包含num个c字符 
h) string s( beg, end ) /// 以区间beg~end(不包含end)内的字符作为字符串s的初值 

string常用函数如下:
a) = , assign() /// 赋以新值 
b) swap() /// 交换两个字符串的内容 
c) +=, append(), push_back() /// 在尾部添加字符 
d) insert() /// 插入字符 
e) erase() /// 删除字符 
f) clear() /// 删除全部字符 
g) replace() /// 替换字符 
h) + /// 串联字符串 
i) ==, !=, <, <=, >, >=, compare() /// 比较字符串 
j) size(), length() /// 返回字符数量 
k) max_size() /// 返回字符的可能最大个数 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值