C++ string

String

查找是否含有子字符串

string::size_type idx;  

    idx=a.find(b);//在a中查找b.

    if(idx == string::npos )//不存在。

        cout << "not found\n";

从指定位置开始查找

a.find(i);//查找 ACSII为i的字符。

string st1("babbabab");
 
cout << st1.find('a', 2) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos
cout << (st1.find('c', 0) == -1) << endl;//1
    cout << (st1.find('c', 0) == 4294967295) << endl;//1   两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
//cout << st1.rfind('a',7) << endl;//6  从指定位置向前查找
/*while(a.find("10") != -1)(是否含有)或

if (a.find("2") != a.npos)

cout << "aaa" << endl;

翻转函数

rerse(mystring.begin(), mystring.end());   头文件?#include <algorithm>

 

删除第几个元素

a.erase(a.begin()+i);

 

替换函数 

用str替换指定字符串从起始位置pos开始长度为len的字符

string ori = "his name is Tom";

string fnd = "Tom";

string rep = "Jack";

ori=ori.replace(ori.find(fnd), fnd.length(), rep); 

 

获取子串

int pos = a.find("-");     返回位置

sub=a.substr(0, pos);  0-pos位置的子串。

std::string str2 = str.substr (3,5);     // "think"

 

  std::size_t pos = str.find("live");      // position of "live" in str

 

  std::string str3 = str.substr (pos);     // get from "live" to the end

string a="hello";

string b=a.substr(1,2); //el  //从1到后面2个字符

a.substr(2); //llo

a.find(a[1],1); //1

 

string 添加字符

str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10,'.');                     // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<<var>int>(5,0x2E);                // "....."

String:

string a = "abc";

char b = 'd';

string c = "ddd";

a.append(c);

a += b;

a += c;

可逆序存储

 

str.clear();  清空

str.empty();  判空

 

int转string
int n = 0;
std::stringstream ss;
std::string str;
ss<<n;
ss>>str;
string转int
std::string str = "123";
int n = atoi(str.c_str());

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值