c++中string类的使用(持续更新)

本文详细介绍了C++中字符串的各种操作方法,包括字符串的创建、转换、查询、修改及删除等核心功能。通过实例展示了如何高效地利用这些方法来处理字符串问题。

ps:string中虽然有push_back();
但是没有push_front();它不能使用前面插入迭代器

1.将整形转为string类型

#include <iostream>
#include <string>
using namespace  std;
int main() {
    int a=12;
    string b;
     b=to_string(a)+"is a string";
    cout<<b;

    return 0;
}

2.将string 转为int型
使用stoi,atoi;

#include <iostream>
#include <string>
using namespace  std;
int main() {
    string s1="123456";
    cout << stoi(s1)+1 << endl;

    return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
    string a;
    a="sfsf";//size是string的成员函数

    cout<<a.size();//表示的不是字符数量,而是字符串占用的字节大小
    cout<<a.length();//也有同样的效果,但是推荐使用size
    //a.会给出成员函数

    //输出固定位置字符
    cout<<a[3]<<endl;//输出第4个字符

    //getline函数接收一行输入
    string str;
    getline(cin,str);
    //若统计数字个数
    int count=0;
    for(size_t i=0;i<str.size();++i){
        if(isdigit(str[i])){
            count++;
        }
    }
    cout<<count<<endl;
    return 0;
}


4.使用某一模块时应将其他模块注释掉,避免互相影响

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str1="123456";
    string str2;
//模块1
    cout<<str2.empty()<<endl;//判断字符串是否为空
    cout<<str1.empty()<<endl;

//模块2
    //清空字符串对象
//    cout<<str1.empty()<<endl;//方法1
//    str1.clear();
//    cout<<str1.empty()<<endl;

//    cout<<str1.empty()<<endl;//方法2
//    str1="";
//    cout<<str1.empty()<<endl;

//模块3
    //str。resize();的使用;
    cout<<str1<<endl;
    cout<<str1.size()<<endl;

    str1.resize(5);//字符串调小
    cout<<str1<<endl;
    cout<<str1.size()<<endl;

    str1.resize(10,'a');//字符串调大,多余位用a填充
    cout<<str1<<endl;
    cout<<str1.size()<<endl;

    str1.resize(15);//字符串调大,多余位用空字符填充
    cout<<str1<<endl;
    cout<<str1.size()<<endl;

//模块4
    //at可以操作字符串指定位置的字符

    cout<<str1.at(0);//输出字符串中的第一个字符
    str1.at(1)='a';//将第二个字符赋值为a
    str1[2]='b';//将第二个字符赋值为a
    //at会检测是否越界,并在越界时抛出异常错误
    //用【】则不会,由于多了检测,所以速度慢一点

    return 0;
}

5.字符串的修改
注:.使用某一模块时应将其他模块注释掉,避免互相影响

#include <iostream>
#include <string>
using namespace std;
int main(){
    //改变字符串内容的相关函数

//模块1
    //末尾添加字符
    string str="123";
    str+='4';//方法1
    cout<<str<<endl;

    str.push_back('5');//方法2
    cout<<str<<endl;

//模块2
    //末尾添加字符串

    string str="123";

    str+="456";//方法1;
    cout<<str<<endl;//123456

    str.append("asd");//方法2
    cout<<str<<endl;//123456asd
    //使用+=更加简单容易理解
    //但是append具有的重载函数是+=无法比的

    //eg1:append(字符串s,开始位置,数量n)将字符串从p开始的n个字符添加到末尾
    str.append("123789",3,3);//其中的字符串可以是自身,也可以是另一字符串
    cout<<str<<endl;//123456asd789

    //eg2:append(数量n,字符c)在末尾添加n个字符c
    str.append(5,'z');
    cout<<str<<endl;

//模块3
    //给字符串重新赋值
    string str="123";

    str="asd";//方法1
    cout<<str<<endl;//asd

    str.assign("qwe");//方法2,使用成员函数
    cout<<str<<endl;//qwe
    //assign()也有两个重载函数
    //eg1:assign(字符串s,开始位置p,数量n)将字符串s从p开始的n个字符赋值给对象字符串
    str.assign("789",1,2);
    cout<<str<<endl;//89

    //eg2:append(数量n,字符c)将字符串赋值为n个字符c
    str.assign(5,'a');
    cout<<str<<endl;//aaaaa

//模块4
    //字符串中插入内容
    string str="1289";
    str.insert(2,"34567");//字符串中位置2及其后面的内容都会向后移动
    cout<<str<<endl;//123456789
    str.insert(0,"asd");//实现在开头插入一个字符串
    cout<<str<<endl;//asd123456789
    str.insert(str.size(),"asd");//实现在末尾插入一个字符串
    cout<<str<<endl;//asd123456789asd

    //此外还可以实现将某字符串特定位置的字符串插入
    string str2="1256";
    str2.insert(2,"asd34asda",3,2);
    cout<<str2<<endl;//123456
    //类似的在字符串中插入n个字符c
    str2.insert(1,2,'0');
    cout<<str2<<endl;//10023456

//模块5
    //字符串的替换
    string str="12389";

    str.replace(3,2,"456");//开始替换位置,替换个数,要替换成的内容
    cout<<str<<endl;//123456

    str.replace(0,3,"1234abc56",4,3);//开始替换位置,替换个数,要替换成的内容所在的字符串,要替换成的内容的开始位置,替换个数
    cout<<str<<endl;//abc456

    //类似的在字符串某位置中替换成n个字符c
    str.replace(0,3,3,'0');
    cout<<str<<endl;//000456

//模块6
    //字符串的删除
    string str="123asd456asd";

    str.erase(3,3);
    cout<<str<<endl;//123456asd

    str.erase(6);//第二个参数不写,默认是把第一个参数后面所有的字符都删除掉
    cout<<str<<endl;//123456
    str.erase(3,100);//第二个参数如果超出范围不会出错
    cout<<str<<endl;//123

    str.erase();//不传参就是默认清空字符串
    cout<<str.empty()<<endl;//1



    return 0;
}

6.字符串中的查找
注:.使用某一模块时应!!将其他模块注释掉!!,避免互相影响

#include <iostream>
#include <string>

using namespace std;
int main() {
    //字符串的查找

//模块1
    //
    string str="12345634";
    //查找字符串
    size_t pos1=str.find("34");//查找某一字符串!!!第一次!!!在该字符串中出现的位置
    cout<<pos1<<endl;//2

    size_t pos2=str.find("34",pos1+1);//查找某一字符串!!!第2次!!!在该字符串中出现的位置
    cout<<pos2<<endl;//6  //只需要从第一次出现的下一位开始查找即可,以此类推

    size_t pos3=str.find("89");//在字符串中是否找到“89”,用bool类型
    cout<<boolalpha<<(pos3==string::npos)<<endl;//ture
    pos3=str.find("56");
    cout<<boolalpha<<(pos3==string ::npos)<<endl;//false
    //查找字符,同理
    size_t pos7=str.find('3');//第一次查找
    cout<<pos7<<endl;//
    size_t pos8=str.find('3',pos7+1);//第2次查找
    cout<<pos8<<endl;//找不到也是返回bool

//模块2
    //rfind.();//从结尾开始找
    string str="12345645";
    size_t pos1=str.rfind("45");//从左往右数,从结尾开始找第一次出现位置
    cout<<pos1<<endl;//6

    size_t pos2=str.rfind("45",pos1-1);//从结尾第一次出现位置
    cout<<pos2<<endl;//3

//模块3
    //find_first_of()//查找某一字符串其中的一个字符在该字符串中第一次出现的位置
    string str="12345645";
    size_t pos1=str.find_first_of ("45");
    cout<<pos1<<endl;//3,找到的是4
    size_t pos2=str.find_first_not_of ("12");//查找不属于某一字符串其中的一个字符在该字符串中第一次出现的位置
    cout<<pos2<<endl;//2,找到的是3
    
    //相反的/find_last_of()查找某一字符串其中的一个字符从后往前在该字符串中第一次出现的位置
    size_t pos3=str.find_last_of ("45");
    cout<<pos3<<endl;//7,找到的是5
    size_t pos4=str.find_last_not_of ("45");//查找不属于某一字符串其中的一个字符从后往前在该字符串中第一次出现的位置
    cout<<pos4<<endl;//5,找到的是6

    return 0;
}

7.字符串的比较

#include <iostream>
#include <string>

using namespace std;
int main() {
    string str1="12345";
    string str2="12345";
    string str3="1234";
    string str4="12355";

    //以bool输出
    cout<<boolalpha;
    cout<<(str1==str2)<<endl;//等于//方法1
    cout<<(str1!=str2)<<endl;//不等于
    cout<<(str1>str3)<<endl;
    cout<<(str1>str4)<<endl;
    cout<<endl;

    cout<<((str1.compare(str2))==0)<<endl;//等于//方法2
    cout<<((str1.compare(str2))!=0)<<endl;//不等于
    cout<<(str1.compare(str3))<<endl;//前置相等,长的字符串大
    cout<<(str1.compare(str4))<<endl;//从第一个不相等的字符开始,
    //str1>str2返回正数
    //str1<str2返回负数
    //str1=str2返回0

    //取出str1的前两个字符作为字符串,与dtr2进行比较
    cout<<str1.compare(0,2,str2)<<endl;//返回值与长度差有关

    //取出str1的前两个字符和str2的后两个字符作为字符串,进行比较
    cout<<str1.compare(0,2,str2,str2.size()-2,2)<<endl;





    return 0;
}

8.关于子字符串

#include <iostream>
#include <string>

using namespace std;
int main() {
    //子字符串
    string str="123456";
    //substr(位置p,数量n)获取在字符串从位置p开始的n个字符作为子字符串
    string str1=str.substr(3,3);//若n大于开始位置起的字符数,获取到末尾会自动停止
    cout<<str1<<endl;//456
    str1=str.substr(2);//不写第二个参数,会默认获取到字符串的末尾
    cout<<str1<<endl;//3456
    str1=str.substr();//不传参默认复制整个字符串
    cout<<str1<<endl;//123456

    //特别的
    string str2=str.substr(str.size(),2);//当第一个参数等于str的长度时,会返回空字符串
    cout<<str2.empty()<<endl;//1

    // string str3=str.substr(str.size()+1,2);//当第一个参数等于str的长度时,会返回空字符串
    ////如果第一个参数大于str的长度时,会抛出错误



    return 0;
}

9.字符串的初始化

#include <iostream>
#include <string>

using namespace std;
int main() {
    //字符串的初始化
    string str;
    cout<<str<<endl;

    string str1="123456";
    cout<<str1<<endl;

    string str2("123");
    cout<<str2<<endl;

    string str3(str1,2,4);
    cout<<str3<<endl;//将字符串str1从位置2开始的4个字符赋值给str3

    string str4(3,'a');
    cout<<str4<<endl;//将n个字符c赋值给str4



    return 0;
}

10.字符串与数值转换

//!!!字符串转数值
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
  cout<<atoi("123")+3<<endl;
  cout<<atol("123")+3<<endl;
  cout<<atoll("123")+3<<endl;
  cout<<atof("123")+3<<endl;

    cout<<atoi("   123")+3<<endl;
    cout<<atol("    123")+3<<endl;
    cout<<atoll("   123")+3<<endl;
    cout<<atof("    123")+3<<endl;

    cout<<atoi("   a123")<<endl;//字符串中含有非数值如字母,就会返回0
    cout<<atol("    a123")<<endl;
    cout<<atoll("   a123")<<endl;
    cout<<atof("    a123")<<endl;

    cout<<atoi("123asd456asd789")+3<<endl;//在遇到第一个数字字符后,会尽可能多的接收连续的数字字符,并转换为整数
    cout<<atol("123asd456asd789")+3<<endl;
    cout<<atoll("123asd456asd789")+3<<endl;
    cout<<atof("123asd456asd789")+3<<endl;
    return 0;
}

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210321152346994.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NlY2FudDAwNw==,size_16,color_FFFFFF,t_70#pic_center在这里插入图片描述数值转字符串

#include <iostream>
#include <cstdio>
using namespace std;
int main() {
//数值转字符串
    //ps:printf是3把文字输出到屏幕上
    //而sprintf把文字输出到字符串缓冲区(字符串缓冲区只能是字符数组)
    int a=1234;
    long long b=1234;
    double c=1234;

    char arr[500]={};
    sprintf(arr,"%d",a);
    cout<<arr<<endl;
    sprintf(arr,"%lld",b);
    cout<<arr<<endl;
    sprintf(arr,"%f",c);
    cout<<arr<<endl;
    //注意,若想要对其做字符串的=操作,只需要申请一个string类的对象并将数组的值赋给字符串即可

    
    return 0;
}

在这里插入图片描述
11.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值