C/C++ 常用字符串处理

1、strcmp 比较字符串大小

str1>str2 返回正整数,str1<str2返回负整数,str1=str2 返回0

#include "stdio.h"
#include "string.h"

int main()
{
	char* str1="abcd";
	char* str2="abcde";
	int result = strcmp(str1,str2);
	printf("result: %d",result);
	return 0;
}

2、字符串转数字

(1)转整数

          atoi(),

#include "stdio.h"
#include "stdlib.h"

int main()
{
	char* str1="1234";
	int a = atoi(str1);
	printf("the result is : %d \n",a);

	return 0;
}

(2)  转浮点型

atof()

#include "stdio.h"
#include "stdlib.h"

int main()
{
	char* str1="1234.12345678912345";
	double a = atof(str1);
	float b=atof(str1);
	printf("the result is : %lf \n",a);
	printf("the result is : %f \n",b);
	return 0;
}

3、取字符串部分内容

find 和 substr

substr(a,b) 取第a位开始长度为b的

#include <string>
#include <iostream>
using namespace std;
using  std::string;
int main()
{
	char* str1="D://abc.jpg";
	string file(str1);
	int p1= file.find(".");
	cout<<p1<<endl;
	int p2=file.find("/");
	cout<<p2<<endl;
	string path=file.substr(0,p2);
	string name=file.substr(p2+2,p1-p2-2);
	string format = file.substr(p1+1);
	cout<<path<<endl;
	cout<<name<<endl;
	cout<<format<<endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值