Ask a questionAsk a question
 

AnswerC++ string question?

  • Saturday, November 07, 2009 4:14 PMharry chan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    C++ string question?

    c++ string question

    a string contain the following:
    abc,def,ghi,jkl
    mnos,sadf,asgs
    f,sa,f,sa,fa,sdf
    how to find each " , " 's location??
    thx

Answers

  • Saturday, November 07, 2009 5:06 PMJijo RajMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello Harry,

    You can use string::find() . Have a look at following code snippet.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[]) 
    {
    	string SearchString = "abc,def,ghi,jkl";
    	string Token = ",";
    
    	size_t Location = 0;
    	Location = SearchString.find( Token );
    
    	while(  string::npos != Location )
    	{
    		cout << "Found at location " << int( Location ) << endl;
    		Location = SearchString.find( Token, Location+1 );
    	}
    
    	return 0;
    }
    
    

    Best Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

All Replies

  • Saturday, November 07, 2009 5:06 PMJijo RajMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello Harry,

    You can use string::find() . Have a look at following code snippet.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[]) 
    {
    	string SearchString = "abc,def,ghi,jkl";
    	string Token = ",";
    
    	size_t Location = 0;
    	Location = SearchString.find( Token );
    
    	while(  string::npos != Location )
    	{
    		cout << "Found at location " << int( Location ) << endl;
    		Location = SearchString.find( Token, Location+1 );
    	}
    
    	return 0;
    }
    
    

    Best Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.