C++字符串处理与转换

string

string字符串处理

长度:str.length()
尺寸:str.size()
比较:str1.compare(str2)
连接:str1+=str2
连接:str1.append(str2)
提取:str2=str2.substr(pos1)
查找:pos=str1.find(str2)
插入:str1.insert(pos1,str2);
替换:str1.replace(pos1,str2);
删除:str1.erase(pos,len)
清除:str.clear()

string转数字

stoi
#include<string>
#include<iostream>
using namespace std;
int main() {
   
	string s = "10";
	int a;
	a = stoi(s, 0, 10);	//把字符串s从p开始转换成b进制的int
	cout << a;
}
stringstream
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
int main() {
   
	string s = "10";
	int a;
	stringstream ss;
	ss << s;
	ss >> a;
	printf("%d", a);
}

数字转字符串:to_string()

#include<string>
#include<cstdio>
using namespace std;
int main() {
   
	int val = 10;
	string s = to_string(val);
	printf("%s", s);
}

string转c字符串

#include<string>
#include<cstdio>
using namespace std;
int main() {
   
	char ar[] = "hello";
	string s = ar;
	printf("%s", s.c_str());
}

C-风格字符串

字符串转数字

atoi
#include<cstdlib>
#include<cstdio>
// ascii to integer/float/long long
int main() {
   
	char num1[] = "123";
	int a = atoi(num1
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值