C++基本操作(一):string

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tonywang1945yes/article/details/78582099
1,统计字符串长度
   int length();
   
2, 判断是否为空
   bool empty();
   
3, 字符串的连接
   string &append(const string &s,int pos,int n);


4,字符串的截取
   string substr(int pos=0,int npos=n)//返回从pos开始的n个字符组成的字符串
   
5,字符串的查找(只举最简单的例子)
   int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
                                                //查找成功时返回所在位置,失败返回string::npos的值 
6,字符串的替换
   string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s  
   
7,字符串的插入
    string &insert(int p0,const string &s);


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

9,读取一行字符串
   cin.getline();
  或者 getline(cin,str);


10, string 转化成 int
   int x = atoi(s.c_str());


11, int 转化成 string
   string s= to_string(x);

猜你喜欢

转载自blog.csdn.net/tonywang1945yes/article/details/78582099