美文网首页
c++ string 操作汇总

c++ string 操作汇总

作者: 江南竹影 | 来源:发表于2018-03-15 17:10 被阅读0次

欢迎访问我的个人博客: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等常见类型转换

相关文章

  • c++ string 操作汇总

    欢迎访问我的个人博客:zengzeyu.com 前言 作为传递信息的载体string数据类型广泛应用于各种编程语言...

  • C++ String操作

    1.初始化 输出: 2.string对象赋值 与初始化不同,使用assign可以修改已经创建的string对象的值...

  • JavaScript中字符串的常见api操作(偏ES5)

    1. api简单汇总 1.1 string相关操作汇总 获取字符串长度 str.length分割字符串 str.s...

  • c++中的字符串string和C语言中的字符char

    c++中的字符串string 在c++中使用string类,必须在程序中包含头文件string #include ...

  • 【C++ STL String】常用操作

    前言 是C++标准程序库中的一个头文件,定义了C++标准中的字符串的基本模板类std::basic_string以...

  • C++ String的常见操作

    如何任意排序String String删除操作任意字符 任意排序 排序我们可以用标准库给我们提供好的算法,或者也可...

  • NDK - JNI java类型转C++

    1、Java String 转 C++ String 2、 java 浮点型数组 转 C++ 浮点型数组 3、 ...

  • 字符串

    C++提供了两种字符串的表示形式: C风格字符 C++引入的string类型 C风格 函数 C++中的String类

  • 高质量C++/C编程指南(转)

    1 有如下的c++类 class String { public: String(const char *str ...

  • String==汇总

    直接去常量池里查找是否有‘abc’,如果没有就创建一个,然后s1指向常量池对应字符 在堆里创建String对象,然...

网友评论

      本文标题:c++ string 操作汇总

      本文链接:https://www.haomeiwen.com/subject/iyphqftx.html