C++ string 操作

c++中对string的split,trim,find操作:

 1 //分割字符串
 2 void splitString(string& word, string p, vector<string>& res){
 3     size_t last = 0;
 4     size_t index = word.find_first_of(p,last);
 5 
 6     string str;
 7     while(index != string::npos){
 8         str = word.substr(last,index-last);
 9         if(str.size() > 0)
10         res.push_back(str);
11         last = index+1;
12         index = word.find_first_of(p,last);
13     }
14 
15     if(index-last > 0){
16         str = word.substr(last,index-last);
17         if(str.size() > 0)
18             res.push_back(str);
19     }
20 }
21 
22 //去掉字符串中的空格
23 void trimString(string& word){
24     string ss = "\n\t\r ";
25     string str;
26     for(size_t i=0; i<ss.size(); i++){
27         str = ss.substr(i,1);
28         while(true){
29             size_t p = word.find(str);
30             if(p != string::npos) word.erase(p,1);
31             else break;
32         }
33         if(word == "") return;
34     }
35 }
36 
37 //计算字符串出现次数
38 int findString(string word, string p){
39     int count = 0;
40     string temp = word;
41     while(true){
42         size_t sp = temp.find(p);
43         if(sp == string::npos) break;
44         count++;
45         temp = word.substr(sp+p.size());
46     }
47 
48     return count;
49 }

 

转载于:https://www.cnblogs.com/leeshine/p/5012249.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值