This is the STL implementation of some of the string processing functions and commonly used string processing techniques, experience is basic error, can be used directly, if there is a problem, can be listed accordingly
Includes: Split
string to int
int to String
Join
#include <string.h>#include <vector>#include <string>//String split function, Python-like split vector<string>SplitCharS[],Const Char*delim) {Char*token = Strtok (S,delim); vector<string>Vstr; while(Token!=null) {stringSTMP = token; Vstr.push_back (STMP); token = strtok (Null,delim); }returnVstr;}//If string is called as follows strings="192.168", sm="."; vector<string>Ss=split (const_cast<Char*> (S.c_str ()), Sm.data ());//parameter is a string version vector<string>SplitstringSstringDelim) {Char*ss=const_cast<Char*> (S.c_str ());Const Char*ddelim= Delim.data ();Char*token = Strtok (Ss,ddelim); vector<string>Vstr; while(Token!=null) {stringSTMP = token; Vstr.push_back (STMP); token = strtok (Null,ddelim); }returnVstr;}//below gives an implementation similar to the Join function in Python, followed by continuous improvementstringJoinConst vector<string>&vs,Const string&s) {intN=vs.size ();stringRESTR; for(intI=0; i<n-1; ++i) Restr=restr+vs[i]+s; Restr +=vs[n-1];returnRestr;}//char * or vector<char> turn string vector<char>Vs;vs.push_back (Char); Vs.push_back (' + ');//Special attention char* turnstrings;s=string(Vs.data ());Chara[ -]="6666666"; s=string(a);//List your own implemented string to int (atoi)intMyatoi (stringSTR) {if(str=="")return 0;if(str.size () = =1) {if(str[0]>' 9 '|| str[0]<' 0 ')return 0; }intbeg=0; while(str[beg]=="') beg++;//Over the preceding empty character if((str[beg]>' 9 '|| str[beg]<' 0 ') &&str[beg]!='-'&&str[beg]!=' + ')return 0;//The first non-null character is illegal intSign= (str[beg]=='-')?-1:1;//Judgment symbol intJ= (str[beg]==' + '|| str[beg]=='-')? beg+1: Beg;//Determine when to start the calculation intres=0; for(intI=j;i<str.size (); ++i) {if(str[i]>' 9 '|| str[i]<' 0 ') Break;//encounters a non-digital that does not look behind the contentRes=res*Ten+ (str[i]-' 0 '); }returnRes*sign;}//list your own implementation of unsigned int to string, no bugstringItostring (unsigned intA) {if(a==0)return "0"; vector<char>vc while(a) {unsigned intTMP = a%Ten; Vc.push_back (tmp+' 0 '); a=a/Ten; }intN=vc.size ();stringS (N,' 0 '); for(inti=n-1; i>=0;-I.) s[n-1-i]=vc[i];returnS }//atoi or itoa (int to string) int atoi (const char *nptr);Char*itoa (intValueChar*string,intRadix);Char*strtok (CharS[],Const Char*delim);//strod string to floating pointstring="3.1415926 This stopped it"; x = Strtod (string, &stopstring);printf("string =%s\n",string);printf("strtod =%f\n", x);printf("Stopped scan at:%s\n", stopstring);string=3.1415926This stopped itstrtod =3.141593Stopped Scan At:this Stopped itstring= -1011This stopped it1. Const Char* andstringConvert1)Const Char* Convert tostring, it can be assigned directly. EX:Const Char* TMP ="Tsinghua".strings = tmp; (2)stringConverted toConst Char*, using C_STR () or data () EX:strings ="Tsinghua";Const Char*tmp = S.c_str ();Const Char*tmp = S.data ();2. Char* andConst Char* Conversion between (1)Const Char* Converted ToChar*, usingconst_cast<Char*> EX:Const Char* TMP ="Tsinghua";Char* p =const_cast<Char*> (TMP);(2)Char* Converted ToConst Char*, can be directly assigned to the value.Char* p ="Tsinghua".Const Char* tmp = p;3. Char* andstringThere is a conversion between the1And2The Foundation,Char* andstringThe transformation is simple. (1)Char* Converted Tostring, it can be assigned directly. EX:Char* p ="Tsinghua".stringstr = p; (2)stringTranslates toChar*, take two steps, firststring-Const Char*, then theConst Char*->Char* EX:stringstr ="Tsinghua";Char* p =const_cast<Char*> (Str.c_str ()):
C + + Common string handling functions-full