【C++】string类对象的字符串修改操作

举报
修修修也 发表于 2024/04/22 20:53:24 2024/04/22
【摘要】 ​📌string类对象的字符串修改操作        string类对象的字符串修改函数共有21个,如下:​编辑​编辑常用的字符串修改操作:函数名称功能说明push_back在字符串后尾插字符cappend在字符串后追加一个字符串operator+=在字符串后追加字符串strc_str返回C格式字符串find+nops从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置rfind...

📌string类对象的字符串修改操作

        string类对象的字符串修改函数共有21个,如下:

编辑

编辑

常用的字符串修改操作:

函数名称

功能说明

push_back

在字符串后尾插字符c

append

在字符串后追加一个字符串

operator+=

在字符串后追加字符串str

c_str

返回C格式字符串

find+nops

从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置

rfind

从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置

substr

在str中从pos位置开始,截取n个字符,然后将其返回

🎏push_back()函数

push_back()函数定义:编辑


使用push_back()函数在string类对象后追加字符:

int main()

{

    string st1("Hello worl");

    cout << st1 << endl;

    cout << st1.size() << endl;

    st1.push_back('d');

    cout << st1 << endl;

    cout << st1.size() << endl;

    return 0;

}


push_back()函数效果如下:​编辑

🎏append()函数

append()函数定义:

append()函数一共实现了7个重载函数,但我们日常对于要在字符串后追加内容的场景使用operator+=的次数更多,因此后面会重点介绍operator+=:

编辑

编辑编辑


使用append()函数在string类对象后追加内容:

int main()

{

    string st1("Hello worl");

    cout << st1 << endl;

    cout << st1.size() << endl;

    st1.append("ddddd!");

    cout << st1 << endl;

    cout << st1.size() << endl;

    return 0;

}

append()函数效果如下:编辑

🎏operator+=()函数

operator+=()函数定义:

编辑


使用operator+=()函数在string类对象后追加string类对象内容:

int main()

{

    string st1("Hello ");

    string st2("world");

    cout << st1 << endl;

    cout << st2 << endl;

    st1 += st2;

    cout << st1 << endl;

    return 0;

}

operator+=()函数效果如下:编辑


使用operator+=()函数在string类对象后追加常量字符串内容:

int main()

{

    string st1("Hello ");

    cout << st1 << endl;

    st1 += "world";

    cout << st1 << endl;

    return 0;

}

operator+=()函数效果如下:编辑


使用operator+=()函数在string类对象后追加常量字符内容:

int main()

{

    string st1("Hello ");

    cout << st1 << endl;

    st1 += '!';

    cout << st1 << endl;

    return 0;

}

operator+=()函数效果如下:编辑

🎏c_str()函数

c_str函数定义:

编辑

使用c_str()函数返回c格式字符串:

c_str主要是考虑到部分项目中不兼容C++只能转换成C的情况,如下,fopen函数不支持传入string类:编辑

如果想要在C++文件中使用C语言函数且参数是string类的话,就可以调用c_str()函数将string类对象转换为C语言字符串:

 

int main()

{

    string filename("Hello");

    fopen(filename.c_str(), "r");

    return 0;

}

c_str()函数效果如下:编辑

🎏find+nops函数

find函数定义:编辑

nops静态成员变量定义:编辑


使用find+nops函数在string类对象pos位置开始向后寻找字符c:

int main()

{

    string st1("https://blog.csdn.net/weixin_72357342?type=blog");

    size_t xpos = st1.find('x');

    if (xpos != string::npos)

    {

        cout << st1[xpos] << endl;

    }

    return 0;

}

find+nops函数效果如下:​编辑


使用find+nops函数在string类对象pos位置开始向后寻找常量字符串:

int main()

{

    string st1("https://blog.csdn.net/weixin_72357342?type=blog");

    size_t npos = st1.find("net");

    if (npos != string::npos)

    {

        cout << st1[npos] << endl;

        cout << st1[npos+1] << endl;

        cout << st1[npos+2] << endl;

    }

    return 0;

}

find+nops函数效果如下:​编辑


使用find+nops函数在string类对象pos位置开始向后寻找string类对象:

int main()

{

    string st1("https://blog.csdn.net/weixin_72357342?type=blog");

    string st2("csdn");

    size_t spos = st1.find(st2);

    if (spos != string::npos)

    {

        cout << st1[spos] << endl;

        cout << st1[spos + 1] << endl;

        cout << st1[spos + 2] << endl;

        cout << st1[spos + 3] << endl;

    }

    return 0;

}

find+nops函数效果如下:编辑

🎏rfind()函数

rfind()函数定义:

编辑


使用rfind()函数在string类对象pos位置开始向前寻找字符b:

int main()

{

    string st1("https://blog.csdn.net/weixin_72357342?type=blog");

    size_t bpos = st1.rfind('b');

    if (bpos != string::npos)

    {

        cout << st1[bpos - 1] << endl;

        cout << st1[bpos] << endl;

        cout << st1[bpos + 1] << endl;

    }

    return 0;

}

rfind()函数效果如下:编辑

•         rfind()函数其余重载函数使用方法和find()函数一样,区别仅在于find()函数从前向后找目标,rfind()函数从后往前找目标.

🎏substr()函数

substr()函数定义:

编辑

使用substr()函数在string类对象中从pos位置开始,截取n个字符并返回:

int main()

{

    string st1("https://blog.csdn.net/weixin_72357342?type=blog");

    string protocol,domain,resource;

    size_t pos1 = st1.find("://");

    size_t pos2 = st1.find('/',pos1+3);

    if (pos1 != string::npos)

    {

        protocol=st1.substr(0, pos1);//后一个参数是要生成的子字符串长度

    }

    cout << protocol << endl;

    if (pos2 != string::npos)

    {

        domain = st1.substr(pos1+3, pos2-(pos1+3));

    }

    cout << domain << endl;

    resource = st1.substr(pos2 + 1, string::npos);

    //最后一个参数给npos或者不给(缺省参数也默认为npos)都代表一直生成到原字符串末尾

    cout << resource << endl;
    return 0;

}

substr()函数效果如下:编辑



【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。