fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6.  
  7. string test(const string &s, char delim, int parseIndex ){
  8. stringstream ss(s);
  9. string parsedStr = "";
  10.  
  11. for( int i = 0; i < (parseIndex+1); i++ ) getline(ss, parsedStr, delim);
  12.  
  13. return parsedStr;
  14. }
  15.  
  16. int main() {
  17. stringstream ss("something without delimiter");
  18. string s1;
  19. getline(ss,s1,';');
  20. cout << "'" << s1 << "'" << endl; //no delim
  21. cout << endl;
  22.  
  23. string s2 = "321;;123";
  24. cout << "'" << test(s2,';',0) << "'" << endl; //classic
  25. cout << "'" << test(s2,';',1) << "'" << endl; //nothing before
  26. cout << "'" << test(s2,';',2) << "'" << endl; //no delim at the end
  27. cout << "'" << test(s2,';',3) << "'" << endl; //this shouldn't be there
  28. cout << endl;
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
'something without delimiter'

'321'
''
'123'
'123'