Code-compiled execution platform: vs2012+win32+debug
Replace all specified substrings in the 1.c++
The following code, as the usual code base reserve, only for the Friends of the Apes to participate:
//replace specified substring string replaceall (const char * src, const Span class= "Hljs-keyword" >string & Target,const string & Subs) {string tmp (SRC); string :: Size_type pos =tmp.find (target), Targetsize =target.size (), ressize = Subs.size (); while (Pos!=string :: NPOs) //found {tmp.replace (pos,targetsize,subs); POS =tmp.find (target, POS + ressize); } return tmp;}
The code mainly describes:
(1) Tmp.find (target): Find the first occurrence of the sub-string subscript;
(2) String::npos: Indicates the value returned when no substring is found.
As stated in MSDN, its value definition is as follows: static const Size_type NPOs =-1, and the conversion to unsigned integer unsignned int represents the maximum number of characters a string can hold.
(3) String::size_type (defined by String Configurator allocator) describes the size of a string, and it is required to be an unsigned integer type.
Because the string configurator defaults to type size_t as Size_type.
2.c++ to cut a string by a specified delimiter
Because Istringstream in C + + cannot provide formatted input of strings by a specified character, it implements a string cut by the specified character and then reads the cut substring.
the//qsort function needs to be compared. Sort in ascending orderintCompConst void*a,Const void*B) {return*(int*) a-* (int*) b;}//Cut a string by the specified delimiter//src: Source string delimiter: Delimiter collection vector<string>SplitConst string& SRC,Const string& delimiter) { vector<string>Strres;intMaxsubstrnum=src.size ();int* pos=New int[Maxsubstrnum];memset(pos,null,maxsubstrnum*sizeof(int));intj=0; for(intI=0; I<delimiter.size (); ++i) {string:: Size_type index=src.find (Delimiter[i]); while(index!=string:: NPOs) {pos[j++]=index; Index=src.find (delimiter[i],index+1); } }//SortQsort (Pos,j,sizeof(int), comp);//Remove First substring stringSUBSTRFIR=SRC.SUBSTR (0, pos[0]);if(substrfir!="") Strres.push_back (Substrfir);//Remove middle j-1 substring for(intI=0; i<j-1; ++i) {stringSubstr=src.substr (pos[i]+1, pos[i+1]-pos[i]-1);if(substr!="") Strres.push_back (SUBSTR); }//Remove last substring stringSubstrlast=src.substr (pos[j-1]+1, Src.size ()-pos[j-1]-1);if(substrlast!="") Strres.push_back (substrlast);Delete[] POS;returnStrres;}
The code mainly describes:
(1) using the Find () and substr () functions to achieve the function of cutting;
(2) in the code. You need to sort the subscripts that appear on the cutter. The cutting Poute of this talent order takes out substrings.
References
[1]http://blog.sina.com.cn/s/blog_49370c500100ov3k.html
[2]http://www.jb51.net/article/55954.htm
C + + implements the cut and replace of strings