C++中的string的一些用法

1、string与字符数组转换

const char* c;
string s="1234";
c = s.c_str(); 

2、char转化为string

char c = 'a';
string s(1,c);

3、截取子串

 std::string str="helloworld";
 std::string str2 = str.substr (3,5); //首位index和长度

4、查找子串

  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");
  std::size_t found = str.find(str2);//返回子串头的位置,rfind从后查找

5、替换子串

 std::string str="this is a test string.";
 std::string str2="n example";
 str.replace(9,5,str2); // "this is an example string." 起始位 替换长度 待替换字符串

6、删除子串

str.erase (10,8);   //起始为、长度
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值