In practice, the string series in c ++ -- string division and replacement (similar to string. split or explode ())

Source: Internet
Author: User
Tags strtok

In practice, the string series in c ++ -- string division and replacement (similar to string. split or explode ())

Splitting a string based on a character is also a common problem in practice and often mentioned during interviews.

If you are a C Sharp programmer, you will know the string. split function, which has the following overloading:
1) public string [] Split (params char [] separator)
2) public string [] Split (char [] separator, int count)
3) public string [] Split (char [] separator, StringSplitOptions options)
4) public string [] Split (string [] separator, StringSplitOptions options)
5) public string [] Split (char [] separator, int count, StringSplitOptions options)
6) public string [] Split (string [] separator, int count, StringSplitOptions options)

If you are a PHP programmer, you will also use the explode method.

However, if you are a C ++ programmer or a C ++ developer, there is no ready-made Splitting Method for strings here. We need to implement it on our own.

Const vector
  
   
Explode (const string & s, const char & c) {string buff {""}; vector
   
    
V; for (auto n: s) {if (n! = C) buff + = n; elseif (n = c & buff! = "") {V. push_back (buff); buff = "" ;}} if (buff! = "") V. push_back (buff); return v;} // use the custom string segmentation function int main () {string str {"the quick brown fox jumps over the lazy dog"}; vector
    
     
V {explode (str, '')}; for (auto n: v) cout <n <endl; return 0 ;}// the output is as follows: thequickbrownfox...
    
   
  

The following is another form:

int split(const string& str, vector
  
   & ret_, string sep = ","){    if (str.empty())    {        return 0;    }    string tmp;    string::size_type pos_begin = str.find_first_not_of(sep);    string::size_type comma_pos = 0;    while (pos_begin != string::npos)    {        comma_pos = str.find(sep, pos_begin);        if (comma_pos != string::npos)        {            tmp = str.substr(pos_begin, comma_pos - pos_begin);            pos_begin = comma_pos + sep.length();        }        else        {            tmp = str.substr(pos_begin);            pos_begin = comma_pos;        }        if (!tmp.empty())        {            ret_.push_back(tmp);            tmp.clear();        }    }    return 0;}
  

========================================================== ==================================
Strings in other languages also have the replace method, so we can also implement this method in c ++:

string replace(const string& str, const string& src, const string& dest){    string ret;    string::size_type pos_begin = 0;    string::size_type pos       = str.find(src);    while (pos != string::npos)    {        cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";        ret.append(str.data() + pos_begin, pos - pos_begin);        ret += dest;        pos_begin = pos + 1;        pos       = str.find(src, pos_begin);    }    if (pos_begin < str.length())    {        ret.append(str.begin() + pos_begin, str.end());    }    return ret;}

========================================================== ======================================
Finally, we will introduce a function in C to intercept strings:
Prototype: extern char * strtok (char * s, char * delim );

#include 
  
   #include 
   
    int main (){  char str[] ="- This, a sample string.";  char * pch;  printf ("Splitting string \"%s\" into tokens:\n",str);  pch = strtok (str," ,.-");  while (pch != NULL)  {    printf ("%s\n",pch);    pch = strtok (NULL, " ,.-");  }  return 0;}
   
  

Output:

Splitting string "-This, a sample string." into tokens:
This
A
Sample
String

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.