C++字符串处理常用函数

it2022-05-05  182

简单总结同时练习打字

#include <cctype>

isalnum()

如果参数是字母或数字,该函数返回true

isalpha()

如果参数是字母,该函数返回真

isdigit()

如果参数是数字(0~9),该函数返回true

toupper();

tolower();

#include <inmanip>

setfill(char c)

就是在预设宽度中如果已存在没用完的宽度大小,则用设置的字符c填充

setw()

int main(){ string str = "12s"; cout<<setfill('@')<<setw(5)<<str<<endl; return 0; }

#include <cstring>

String::insert()

插入函数

String::substr()

截取函数

String::erase()

擦除函数

#include <algorithm>

 reverse(str.begin() ,str.end());

字符串翻转

int main(){ string str = "12s"; reverse(str.begin() ,str.end() ); cout<<str; return 0; }

#include <sstream>

sstream有三种类:ostringstream:用于输出操作,istringstream:用于输入操作,stringstream:用于输入输出操作

stringstream

依次输出字符

#include <stdio.h> #include <math.h> #include <sstream> #include <iostream> using namespace std; int main(){ string str ="Zhu Jianfeng 123"; stringstream st; st<<str; string temp; st>>temp; cout<<temp<<endl; st>>temp; cout<<temp<<endl; return 0; }

#include <stdio.h>

sscanf()

用于从字符串中读取指定格式的数据

#include <stdio.h> int main(void){ char str[100] ="123568qwerSDDAE"; char lowercase[100]; int num; sscanf(str,"%d %[a-z]", &num, lowercase); printf("The number is: %d.\n", num); printf("The lowercase is: %s.", lowercase); return 0; }

sprintf() 

#include <stdio.h> #include <math.h> int main() { char str[80]; double M_PI = 3.14159; sprintf(str, "Pi 的值 = %f", M_PI); puts(str); return(0); }

 

 

 

 

 

 

 

 


最新回复(0)