Find,substr and Getline features in C + +

Source: Internet
Author: User
Tags strtok

The function of split in C + + is first to understand several functions

Find_first_of or find_first_not_of in C + + with string itself

Find_last_of or Find_last_not_of

Function prototype: can be used to search for strings, Char,char * Three types

String (1)
size_t find_first_of (const string& str, size_t pos = 0) const;
C-string (2)
size_t find_first_of (const char* s, size_t pos = 0) const;
Buffer (3)
size_t find_first_of (const char* S, size_t pos, size_t N) const;
Character (4)
size_t find_first_of (char c, size_t pos = 0) const;

Secondly, the SUBSTR function in string, according to the position obtained above, and then call the SUBSTR function can intercept the string within the set interval, if Len is not specified, then directly to the tail;

String substr (size_t pos = 0, size_t len = npos) const;


With find and substr can be implemented Getline word segmentation function, previously only thought to be a read string, the function can follow the specified delimiter to read the string stream;
istream& Getline (istream& is, string& str, char Delim)
istream& Getline (istream& is, string& str);

To implement the parse URL:

Code: URL is http://www.baidu.com:80/query?k1=v1&k2=v2
  1. void parseURL (String &url) {
  2. StringStream SS (URL); Sstream header File
  3. String field;
  4. while (Getline (Ss,field, ': ')) {
  5. Res.push_back (field);
  6. cout<<field<<endl;
  7. }
  8. String host = Res[1];
  9. int pos = host.find_first_not_of ('/');
  10. Host = Host.substr (Pos,host.size ()-pos);
  11. cout<
  12. String port = res[2];
  13. pos = port.find_first_of ('/');
  14. Port = port.substr (0,pos);
  15. cout<<port<<endl;
  16. string query = Res[2].substr (pos+1);
  17. pos = query.find_first_of ('? ');
  18. String nquery = Query.substr (0,pos);
  19. cout<<nquery<<endl;
  20. string params = Query.substr (pos+1);
  21. cout<<params<<endl;
  22. StringStream Parass (params);
  23. while (Getline (Parass,field, ' & ')) {
  24. pos = field.find_first_of (' = ');
  25. String key = Field.substr (0,pos);
  26. String val = Field.substr (pos+1);
  27. cout<< "key =" <<key<< "val =" << val<<endl;
  28. }
  29. }

By the way, there are similar features in the C language: Strtok and Strtok_r, for example.

The function prototypes were:
Char *strtok (char *s, char *delim);Char *strtok_r (char * Str, const char * Delim, char * * saveptr); Thread-Safe Edition



Find,substr and Getline features in C + +

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.