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;
}