温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++怎么实现字符串切割

发布时间:2022-07-01 10:08:07 来源:亿速云 阅读:91 作者:iii 栏目:开发技术

本篇内容介绍了“C++怎么实现字符串切割”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    字符串切割的两种方法

    字符串切割的使用频率还是挺高的,string本身没有提供切割的方法,但可以使用stl提供的封装进行实现或者通过c函数strtok()函数实现。

    1、通过stl实现

    涉及到string类的两个函数find和substr:

    1、find函数

    • 原型:size_t find ( const string& str, size_t pos = 0 ) const;

    • 功能:查找子字符串第一次出现的位置。

    • 参数说明:str为子字符串,pos为初始查找位置。

    • 返回值:找到的话返回第一次出现的位置,否则返回string::npos

    2、substr函数

    • 原型:string substr ( size_t pos = 0, size_t len = npos ) const;

    • 功能:获得子字符串。

    • 参数说明:pos为起始位置(默认为0),len为字符串长度(默认为npos)

    • 返回值:子字符串

    代码如下:

    std::vector<std::string> splitWithStl(const std::string &str,const std::string &pattern)
    {
        std::vector<std::string> resVec;
        if ("" == str)
        {
            return resVec;
        }
        //方便截取最后一段数据
        std::string strs = str + pattern;
        
        size_t pos = strs.find(pattern);
        size_t size = strs.size();
        while (pos != std::string::npos)
        {
            std::string x = strs.substr(0,pos);
            resVec.push_back(x);
            strs = strs.substr(pos+1,size);
            pos = strs.find(pattern);
        }
        
        return resVec;
    }

    2、通过使用strtok()函数实现

    • 原型:char *strtok(char *str, const char *delim);

    • 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。

    • 描述:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时 则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。

    • 其它:strtok函数线程不安全,可以使用strtok_r替代。

    代码如下:

    std::vector<std::string> split(const std::string &str,const std::string &pattern)
    {
        //const char* convert to char*
        char * strc = new char[strlen(str.c_str())+1];
        strcpy(strc, str.c_str());
        std::vector<std::string> resultVec;
        char* tmpStr = strtok(strc, pattern.c_str());
        while (tmpStr != NULL)
        {
            resultVec.push_back(std::string(tmpStr));
            tmpStr = strtok(NULL, pattern.c_str());
        }
        
        delete[] strc;
        
        return resultVec;
    };

    字符串分割&类型转换(string->double)

    【自己备用】

    代码如下(示例):

    #include<sstring>//头文件
    #include<iostream>
    using namespace std;
    int main()
    {
        string line; 
        ifstream is("2011_6.txt");
        while(is>>line)
        {
            cout<<line<<endl;
            istringstream   is1(line.substr(line.find("C")+2,line.find(",")-2));   //创建一个istringstream对象,目的是将()中的字符串转换为数字型
            // cout<<line.find("C")<<"    "<<line.find(",")<<endl;
            double o_x, o_y, r;
            is1>>o_x;         //将转换后的数字输入o_x
            cout<<o_x<<endl;
            line.erase(line.find("C"),line.find(",")+1);    //将字符串中已经用过的部分擦除,为后面的字符串处理提供便利
            cout<<line<<endl;
            //cout<<line.find(",")<<endl;
            istringstream is2(line.substr(0,line.find(",")));
            is2>>o_y;
            cout<<o_y<<endl;
            line.erase(0,line.find(",")+1);
            cout<<line<<endl;
            istringstream is3(line.substr(0,line.find(";")));
            is3>>r;
            cout<<r<<endl;
            line.erase(0,line.find(";")+1);
            cout<<line<<endl;
        }
    }
    • substr(m,n)表示从位置m开始截取n个字符,返回字符串,m默认0

    • erase(m,n) 表示从位置m开始擦除n个字符,返回字符串,m默认0

    • find(字符a)表示返回字符a所在的位置

    “C++怎么实现字符串切割”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    c++
    AI