刷题日记--易忘汇总(string篇)

String型常见操作

1.C++ string整行输入

C++中无法使用gets来获取一整行,因为在C11标准中已被正式删除,此时需要操作 getline()来实现输入(包含头文件#include )

#include <string>
int main()
{
	string st;
	getline(cin,st);
	cout<<st<<endl;
	return 0;
}

2. String 类型与数字的转换

数字转string

头文件:string.h

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int num=123;
    string str=to_string(num);
    cout<<str<<endl;
    //输出结果为 123
   return 0;
}

ps:此方法仅在力扣有效,cb与dev都报错,可以改为

int a = 23;
stringstream ss;
ss << a;
string s1 = ss.str();

string转数字

头文件:cstdlib

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int num=0;
    string str="123";
    num=stoi(str);
    cout<<num<<endl;
   return 0;
}

ps:此方法仅在力扣有效,cb与dev都报错,可以改为

#include<bits/stdc++.h>
using namespace std;
int main()
{
    std::string s = "152";
    std::stringstream ss;
    //方法一: 
    int num1 = std::atoi( s.c_str() );
    cout<<num1<<endl;
     
    //方法二:
    int num2;
    ss<<s;
    ss>>num2;
    cout<<num2<<endl; 
    return 0;
}

3. string去除前导0

//s为string
while(s[0]=='0'){
	s.erase(0);
}

4. C++仿写java replaceAll

将base中所有src替换为dst

//替换空格,(-2)->(0-2),(+4)->(0+4) 因为c++没有replaceAll,只能自己手写
    string replace(string& base, string src, string dst) {
        int pos = 0, srclen = src.size(), dstlen = dst.size();
        while ((pos = base.find(src, pos)) != string::npos) {
            base.replace(pos, srclen, dst);
            pos += dstlen;
        }
        return base;
    }

用str替换指定字符串从起始位置pos开始长度为len的字符

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "he is@ a@ good boy";
	str=str.replace(str.find("a"),2,"#");  //从第一个a位置开始的两个字符替换成#
	cout<<str<<endl; 
	//结果:he is@ # good boy
	return 0;
}

用str替换 迭代器起始位置 和 结束位置 的字符

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "he is@ a@ good boy";
	 str=str.replace(str.begin(),str.begin()+5,"#"); //用#替换从begin位置开始的5个字符
	 cout<<str<<endl;
	 //结果:#@ a@ good boy
	 return 0; 
}

用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "he is@ a@ good boy";
	string substr = "12345";
	str=str.replace(0,5,substr,substr.find("1"),4); //用substr的指定字符串替换str指定字符串
	cout << str << endl;
	//结果:1234@ a@ good boy
	return 0; 
}

5. string字符串拼接

string s1 = "Hello ";
string s2 = "World!";
s1.append(s2);
cout << s1 << endl;
 //结果:Hello World!
 
string s3 = "Hello ";
string s4 = "Hello World!";
//从s4的第六位(0位开始数)开始的连续5位--World,即s3+"World"
s3.append(s4,6,5);
cout << s3 << endl;
 //结果:Hello World
 
string s5 = "Hello ";
//将10个A拼接到字符串s5的后面
s5.append(10,'A');
cout << s5 << endl;
//结果:Hello AAAAAAAAAA
 
string s6 = s1 + s2;
cout << s6 <<endl;
//结果:Hello World!World!

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爆零选手George

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值