Use STL string
1. Search for characters
STD: wstring strdata = l "<result> [Beijing, Shanghai, Shenzhen] </result>"
Void DOF (const STD: wstring & strdata, STD: List <STD: wstring> & listdo)
{
STD: wstring strtarb = l "<result> [";
STD: wstring strtare = l "] </result> ";
STD: wstring: size_type nbegintempb = 0, nendtempb = 0, nbegintempe = 0, nendtempe = 0;
Nendtempb = strdata. Find (strtarb, nbegintempb );
Nendtempe = strdata. Find (strtare, nbegintempe );
If (nendtempb! = STD: wstring: NPOs & nendtempe! = STD: wstring: NPOs & nendtempe> nendtempb + strtarb. Length ())
{
STD: wstring strtemp = strdata. substr (nendtempb + strtarb. Length (), nendtempe-nendtempb-strtarb. Length ());
STD: wstring strsub;
Strtemp + = L ",";
STD: List <STD: wstring >:: iterator it;
Listdo. Clear ();
STD: wstring: size_type nbegin = 0, nend = 0;
While (nend = strtemp. Find (L ",", nbegin ))! = STD: wstring: NPOs)
{
Strsub = strtemp. substr (nbegin, nend-nbegin );
If (strsub. Length ()> 0)
{
Listdo. push_back (strsub );
}
Nbegin = nend + 1;
}
}
}
2. Replace string
The string in STL only provides the replace function based on the position and interval, but cannot replace another string in the specified string with one string.
Void string_replace (STD: string & strbig, const STD: string & strsrc, const STD: string & strdst)
{
STD: String: size_type Pos = 0;
While (Pos = strbig. Find (strsrc, POS ))! = String: NPOs)
{
Strbig. Replace (Pos, strsrc. Length (), strdst );
Pos + = strdst. Length ();
}
}
// Use
STD: String strinfo = "this is winter, winter is a programmer. Do you know Winter? ";
// Replace all "Winter" in strinfo with "Wende"
string_replace (strinfo, "Winter", "Wende");