总结一下string函数中的一些常用用法

参考博客  感谢!

http://blog.csdn.net/saya_/article/details/47378239


下面讲述的都是一些常用的函数:

插入 insert  查找 find 删除 erase 取子串 substr 替换 replace

1.插入  insert

str.insert(6," ") 在第6个位置插入一个字符串(在这里字符串我给省略了)

    str.insert(6,str3,3,4) 在 str串中的第6个位置插入str3的串 从3位置开始 往后4个字符

例子:

#include <iostream>  
#include <string>  
  
int main ()  
{  
  std::string str="to be question";  
  std::string str2="the ";  
  std::string str3="or not to be";  
  std::string::iterator it;  
  
  // used in the same order as described above:  
  str.insert(6,str2);                 // to be (the )question在str的第6个位置插入str2  
  str.insert(6,str3,3,4);             // to be (not )the question在str的第6个位置插入str3的一个片段,为下标为3开始往后的4个字符  
  str.insert(10,"that is cool",8);    // to be not (that is )the question  
  str.insert(10,"to be ");            // to be not (to be )that is the question  
  str.insert(15,3,':');               // to be not to be(:::) that is the question  
  std::cout << str << '\n';  
  return 0;  
}  
2 .查找 find 和rfind   这里只说这两个

find  是找到串中第一个和要查找的字符串(也可以是单个字符) 并返回其第一个字符的下标

rfind  是找到串中最后一个和要查找的字符串(也可以是单个字符) 并返回其第一个字符的下标

   

#include<iostream>  
#include<string.h>  
#include<string>  
using namespace std;  
int main()  
{  
    string str1="456456";  
  
    string str2="456";  
    string str3="654";  
  
    int a=str1.rfind(str2);  
    int b=str1.rfind(str3);  
  
    cout<<a<<endl;//输出3  
    cout<<b<<endl;//输出-1  
    return 0;  
}  

#include<iostream>  
#include<string>  
#include<algorithm>//find的头文件  
using namespace std;  
int main()  
{  
    string str1 = "123456";  
    //第一个是string中找string  
    string str2 = "34";//能找到的  
    string str3 = "364";//找不到的  
  
    int a = str1.find(str2);  
    int b = str1.find(str3);  
    cout << a << endl;  
    cout << b << endl;  
    /* 
    输出是: 
             2 
            -1 
    */  
  
  
    //第二个是string中找char  
    char ch1 = '3';//能找到的  
    char ch2 = '9';//找不到的  
  
    int aa = str1.find(ch1);  
    int bb = str1.find(ch2);  
  
    cout << aa << endl;  
    cout << bb << endl;  
    /* 
    输出是: 
           2 
           -1 
    */  
    return 0;  
}  


3.删除 erase

erase(1,10)  从1开始 往后删除字符串 长度为10

// string::erase  
#include <iostream>  
#include <string>  
int main()  
{  
    std::string str("This is an example sentence.");  
    std::cout << str << '\n';  
    // "This is an example sentence."  
    str.erase(10, 8);                          
    std::cout << str << '\n';  
    // "This is an sentence."   
    std::cout << str << '\n';  
    // "This is a sentence."  
    str.erase(str.begin() + 5, str.end() - 9);   
    std::cout << str << '\n';  
    // "This sentence."  
    return 0;  
}  


4.substr

s.substr(3,5)  从3开始取长度为5的字串

// string::substr  
#include <iostream>  
#include <string>  
  
int main ()  
{  
  std::string str="We think in generalities, but we live in details.";  
                  
  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  
  std::cout << str2 << ' ' << str3 << '\n';//输出:think live in details.  
  return 0;  
}  

5.替换 repalce

 str.replace(7, 5, "left");//按地址从&str[7]到&str[12]的内容替换为left  

replace(myvector.begin(), myvector.end(), 20, 99); 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值