c++ string 类型操作汇总

8 篇文章 0 订阅
欢迎访问我的个人博客:zengzeyu.com 前言作为传递信息的载体string数据类型广泛应用于各种编程语言中,尤其在轻量化语言 Python 中发挥的淋漓尽致,通过string传递信息 Python 使各个模块组装在一起完成指定的工作任务,这也是 Python 被称为“胶水语言”的原因。 本文着眼于 c++ 中string的应用。首先建议先浏览string在线文档,这其中包...
摘要由CSDN通过智能技术生成

欢迎访问我的个人博客:zengzeyu.com

前言


作为传递信息的载体string数据类型广泛应用于各种编程语言中,尤其在轻量化语言 Python 中发挥的淋漓尽致,通过string传递信息 Python 使各个模块组装在一起完成指定的工作任务,这也是 Python 被称为“胶水语言”的原因。
本文着眼于 c++string的应用。首先建议先浏览string在线文档,这其中包含了大多数日常所需函数。

正文


1. string 内查找字符串str

std::string::find(str)函数:http://www.cplusplus.com/reference/string/string/find/
Example

// string::find
#include <iostream>       // std::cout
#include <string>         // std::string

int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';

  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  std::cout << str << '\n';

  return 0;
}

其中 npos 值为 -1,用于判断。
其他查找功能类函数:
- rfind: 找到目标str最后一次出现位置
- find_first_of: 从起始位置查找目标str
- find_last_of: 从结束位置往回查找目标str

2. string内删除目标str

substr: 本质还是查找目标str
Example

// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  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';

  return 0;
}

3. 字符串分割

字符串分割推荐使用 std::stringstream 作为中间载体和 getline() 函数组合来完成。
以分割字符 “ ; ”为例:

void splitPathStr(std::string& path_str)
{
    std::vector<std::string> filelists;
    std::stringstream sstr( path_str );
    std::string token;
    while(getline(sstr, token, ';'))
    {
        filelists.push_back(token);
    }
}

4. int型与string型互相转换

int型转string

void int2str(const int &int_temp,string &string_temp)  
{  
        stringstream stream;  
        stream<<int_temp;  
        string_temp=stream.str();   //此处也可以用 stream>>string_temp  
}  

string型转int

void str2int(int &int_temp,const string &string_temp)  
{  
    stringstream stream(string_temp);  
    stream>>int_temp;  
} 

未完待续…


参考文献:
1. c++ reference
2. C++中int、string等常见类型转换

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值