Use STL string

Source: Internet
Author: User

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");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.