Spliting strings?

This is a discussion on Spliting strings? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved A stringstream would be ideal here - and much simpler than your current code. There are ...

  1. #16
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by Daved
    A stringstream would be ideal here - and much simpler than your current code. There are a lot of existing string functions that you aren't taking advantage of. For example, there is no need to use strlen with a C++ string. It has a length() method.

    BTW, using dwks code above to copy a C++ string into a C style string needs to be strcpy(newbuffer, charString.c_str());
    Sorry if this is asking to much, but could you show me an example of using stringstream. Iv been going by what everyone tell's me, and I always get shifted in diffrent directions.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It does?

    You got me there. Sorry.

    >> Sorry if this is asking to much, but could you show me an example of using stringstream. Iv been going by what everyone tell's me, and I always get shifted in diffrent directions.

    Different people have their own ways of doing things which unfortunately makes it confusing. You just need to try to understand what's going on as much as possible and make the decision on what sounds like the best way to go.

    here is an example using stringstreams. Compile it and run it to see what is going on:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
     
    int main()
    {
    	std::string line = "This is a line of text.";
    	std::istringstream istrm(line);
    	std::string word;
    	while (istrm >> word)
    		std::cout << word << std::endl;
    	std::cout << "---Done---" << std::endl;
     
    	line = "This,is,a,line,of,text,with,commas";
    	istrm.clear();
    	istrm.str(line);
    	while (getline(istrm, word, ','))
    		std::cout << word << std::endl;
    	std::cout << "---Done---" << std::endl;
     
    	line = "A Number is 13.5";
    	istrm.clear();
    	istrm.str(line);
    	istrm >> word;
    	std::cout << word << std::endl;
    	istrm >> word;
    	std::cout << word << std::endl;
    	istrm >> word;
    	std::cout << word << std::endl;
     
    	double number;
    	istrm >> number;
    	std::cout << number + 1.0 << std::endl;
     
    	std::cout << "---Done---" << std::endl;
    }

  3. #18
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    theres always boost::tokeniser you can get it at here
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #19
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Different people have their own ways of doing things which unfortunately makes it confusing. You just need to try to understand what's going on as much as possible and make the decision on what sounds like the best way to go.

    here is an example using stringstreams. Compile it and run it to see what is going on:directions.
    Ok thanks, but there's one little part im confused. Where you have
    Code:
    std::istringstream istrm(line);
    I understand thats a case for the istringstream class, but whats with the (line) part? Sorry if this is a stupid question . I bought a 1000 page book "Sam's Teach Yourself C++ in 21 Days". It wasn't specific on these topics.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    When you construct an input string stream, it should have some data in it to extract into your variables. It's just like an ifstream that has the data in the file that can be extracted into variables. The constructor of the istringstream takes a string that you will parse. So I created an example string to parse ("This is a line of text.") and passed that string to the constructor of the istringstream. So now the istringstream is loaded with that string and you extract data out of it just like it was a file with that data inside it.

    Notice later I called istrm.str(line). This basically does the same thing, except it is for a stream instance that has already been constructed. It resets the internal contents of the stream to whatever line holds.

    If you understand file streams, just think of a stringstream as the same thing, except instead of an actual file on disk, there is just a big string in memory that holds all the data and can be accessed bythe str() method.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. spliting strings
    By tyroiusrtmaonm in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2007, 12:21 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21