2uzhan.com
Advertisement
Now Place:2uzhan.com » using stringstream for tokenizing a string

using stringstream for tokenizing a string

Software Development @ July 10, 2005   Views:0

if the token is separated by character like ':' or '|' how can you use sstream to tokenize it?

<< moderator edit: split thread from [thread=27724]here[/thread] >>

--------------Solutions-------------

#include <iostream>
#include <string>
#include <sstream>

int main()
{
   std::string token, text("Here:is:some:text");
   std::istringstream iss(text);
   while ( getline(iss, token, ':') )
   {
      std::cout << token << std::endl;
   }
   return 0;
}

/* my output
Here
is
some
text
*/

Pretty neat solution, thanks.
Here is a Tokenizer class from Codeproject, which can tokenize string separated by
different chars like space, coma, predefined char group etc.

//Tokenizer.h

#ifndef __TOKENIZER_H__
#define __TOKENIZER_H__

#include <functional>
#include <string>
#include <vector>
#include <algorithm>
#include <locale>

using namespace std;

//For the case the default is a space.
//This is the default predicate for the Tokenize() function.
class CIsSpace : public unary_function<char, bool>
{
public:
  bool operator()(char c) const;
};

inline bool CIsSpace::operator()(char c) const
{
  //isspace<char> returns true if c is a white-space character (0x09-0x0D or 0x20)
  //return isspace<char>(c);
  return isspace(c);
}

//For the case the separator is a comma
class CIsComma : public unary_function<char, bool>
{
public:
  bool operator()(char c) const;
};

inline bool CIsComma::operator()(char c) const
{
  return (',' == c);
}

//For the case the separator is a character from a set of characters given in a string
class CIsFromString : public unary_function<char, bool>
{
public:
  //Constructor specifying the separators
  CIsFromString::CIsFromString(string const& rostr) : m_ostr(rostr) {}
  bool operator()(char c) const;

private:
  string m_ostr;
};

inline bool CIsFromString::operator()(char c) const
{
  int iFind = m_ostr.find(c);
  if(iFind != string::npos)
    return true;
  else
    return false;
}

//String Tokenizer
template <class Pred=CIsSpace>
class CTokenizer
{
public:
  //The predicate should evaluate to true when applied to a separator.
  static void Tokenize(vector<string>& roResult, string const& rostr, Pred const& roPred=Pred());
};

//The predicate should evaluate to true when applied to a separator.
template <class Pred>
inline void CTokenizer<Pred>::Tokenize(vector<string>& roResult, string const& rostr, Pred const& roPred)
{
  //First clear the results vector
  roResult.clear();
  string::const_iterator it = rostr.begin();
  string::const_iterator itTokenEnd = rostr.begin();
  while(it != rostr.end())
  {
    //Eat seperators
    while(roPred(*it))
      it++;
    //Find next token
    itTokenEnd = find_if(it, rostr.end(), roPred);
    //Append token to result
    if(it < itTokenEnd)
      roResult.push_back(string(it, itTokenEnd));
    it = itTokenEnd;
  }
}
#endif //__TOKENIZER_H__

To use it:

vector<string> oResult;
      CTokenizer<CIsFromString>::Tokenize(oResult, path, CIsFromString(":"));
      for(int i=0; i<oResult.size(); i++)   cout << oResult[i] << endl;

Write a java program that asks a person for his height (in meters) and weight (in kilograms) and
outputs one of the messages: underweight, normal, or obese, using the BMI ...

I'm attempting to simplify my fundraising page, and would like to show exactly how much each donation would help us out. I've listed the options below, but I would like ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...
Tags:
© 2018 2uzhan.com Contact