Implement the string split function of c ++ to implement cstringsplit

Source: Internet
Author: User

Implement the string split function of c ++ to implement cstringsplit

Today, I wrote a program and encountered a function to implement string. split. Python, qt, and c ++. Copy one from the Internet and put it here. If you need it, you can drag it and use it directly. Otherwise, you will get rid of it when you write a small example.

For example, "aa * bb * cc" is stored as a vector <string> "aa" "bb" "cc"

// Temp1.cpp: defines the entry point of the console application.
//

# Include "stdafx. h"
# Include <iostream>
# Include <stdio. h>
# Include <string>
# Include <vector>
Using namespace std;

 

Int _ tmain (int argc, _ TCHAR * argv [])
{
Char a [] = "abc * 123 * xyz"; // The object is parsed to abc 123 xyz and then stored in the following variable vector <string>
String strArry =;
Vector <string> strArryList;


Size_t last = 0;
Size_t index = strArry. find_first_of ("*", last); // find the first one after the last coordinate *
While (index! = Std: string: npos) // locate a push vector until the final point is found.
{
StrArryList. push_back (strArry. substr (last, index-last ));
Last = index + 1;
Index = strArry. find_first_of ("*", last );
}
If (index-last> 0) // remember to push the last forward. Here is "xyz"
{
StrArryList. push_back (strArry. substr (last, index-last ));
}


For (int I = 0; I <strArryList. size (); I ++)
Std: cout <strArryList [I] <std: endl;
Getchar ();
Return 0;
}

 

Related Article

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.