0

Possible Duplicate:
How do I tokenize a string in C++?

hello every one i want to divide my string into two parts based on '\t' is there any built in function i tried strtok but it take char * as first in put but my variable is of type string thanks

Community
  • 1
  • 1
tariq
  • 1
  • 3
  • duplicate of http://stackoverflow.com/questions/5502368/c-how-to-tokenize-this-string and http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c – Tony May 08 '11 at 09:28

2 Answers2

2
#include <sstream>
#include <vector>
#include <string>

int main() {
   std::string str("abc\tdef");
   char split_char = '\t';
   std::istringstream split(str);
   std::vector<std::string> token;

   for(std::string each; std::getline(split, each, split_char); token.push_back(each));
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Why can't you use C standard library?

Variant 1. Use std::string::c_str() function to convert a std::string to a C-string (char *)

Variant 2. Use std::string::find(char, size_t) to find a necessary symbol ('\t' in your case) than make a new string with std::string::substr. Loop saving a 'current position' till the end of line.

korreg
  • 1