Hi all,
I have following string
0x0002,A5651QPR87GBZ094RTF52,D,A,000001,ABC ,10000.00 , EOT
i need to split the above using ',' as the delimiter & put those in to the following string variables.
1.Command
2.Key
3.Msgtype
4.number
5.name
6.amount
pls reply me as sap.
Regards

int main()
{
    string str = "0x0002,A5651QPR87GBZ094RTF52,D,A,000001,ABC ,10000.00 , EOT";
    string word;
    stringstream stream(str);
    while( getline(stream, word, ',') )
        cout << word << "\n";
}

Nothing magical going on here, but in an uncharacteristic move, I actually added comments. Yes, I'm bored:

#include <string>
#include <vector>

//! Maintains a collection of substrings that are
//! delimited by a string of one or more characters
class Splitter {
  //! Contains the split tokens
  std::vector<std::string> _tokens;
public:
  //! Subscript type for use with operator[]
  typedef std::vector<std::string>::size_type size_type;
public:
  //! Create and initialize a new Splitter
  //!
  //! \param[in] src The string to split
  //! \param[in] delim The delimiter to split the string around
  Splitter ( const std::string& src, const std::string& delim )
  {
    reset ( src, delim );
  }

  //! Retrieve a split token at the specified index
  //!
  //! \param[in] i The index to search for a token
  //! \return The token at the specified index
  //! \throw std::out_of_range If the index is invalid
  std::string& operator[] ( size_type i )
  {
    return _tokens.at ( i );
  }

  //! Retrieve the number of split tokens
  //!
  //! \return The number of split tokesn
  size_type size() const
  {
    return _tokens.size();
  }

  //! Re-initialize with a new soruce and delimiter
  //!
  //! \param[in] src The string to split
  //! \param[in] delim The delimiter to split the string around
  void reset ( const std::string& src, const std::string& delim )
  {
    std::vector<std::string> tokens;
    std::string::size_type start = 0;
    std::string::size_type end;

    for ( ; ; ) {
      end = src.find ( delim, start );
      tokens.push_back ( src.substr ( start, end - start ) );

      // We just copied the last token
      if ( end == std::string::npos )
        break;

      // Exclude the delimiter in the next search
      start = end + delim.size();
    }

    _tokens.swap ( tokens );
  }
};

#include <iostream>

int main()
{
  const std::string line =
    "0x0002,A5651QPR87GBZ094RTF52,D,A,000001,ABC ,10000.00 , EOT";

  Splitter split ( line, "," );

  for ( Splitter::size_type i = 0; i < split.size(); i++ )
    std::cout<<'|'<< split[i] <<"|\n";
}
#include <iostream>
#include <cstring>
using namespace std;

int main() {
	char str[] = "0x0002,A5651QPR87GBZ094RTF52,D,A,000001,ABC ,10000.00 , EOT";
	char* pch;
	pch = strtok(str, ", ");
	while (pch != NULL) {
		cout << pch << endl;
		pch = strtok(NULL, ", ");
	}
	return 0;
}

>OMG Narue, why did you make that soooo difficult ??
>See my post if you missed it.
I didn't make it difficult, and I did see your post. Do please modify your code to accept "::" as a delimiter to see where I was going with my class. ;)

Hi all thanks a lot. i managed to modify it as follows.it worked.
int main()
{
string str = "0x0002,A5651QPR87GBZ094RTF52,D,A,000001,ABC ,10000.00 , EOT";

stringstream stream;
stream<<str
}

This article has been dead for over six months. Start a new discussion instead.