wxStringTokenizer, can't get it work properly Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

wxStringTokenizer, can't get it work properly

Post by Sunsawe » Tue Jul 10, 2007 10:38 pm

Hi all,

I'm using that code to split a text in part with empty line as separator

Code: Select all

wxStringTokenizer tkz(text, wxT("\r\n\r\n"));
	

	while ( tkz.HasMoreTokens() )
	{
		wxString token = tkz.GetNextToken();
		// process token here
	}
But with that, it splits the text at each line ( so like if "\r\n" was the separator).

Can someone tell me what i'm doing wrong?

Thanks

PS: i'm using wxWidgets 2.8.3 if that can help.

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Re: wxStringTokenizer, can't get it work properly

Post by mc2r » Tue Jul 10, 2007 10:48 pm

I believe that the list of delimiters is just that a list. so \r\n\r\n doesn't match the string \r\n\r\n but either a \r or a \n with both being added a redundant second time.

You will have to check the return'd token to see if it is an empty string if you only want empty lines.

Code: Select all

wxStringTokenizer tkz(text, wxT("\r\n"));
	

	while ( tkz.HasMoreTokens() )
	{
		wxString token = tkz.GetNextToken();
                if(token == wxEmptyString){
		    // process token here
                 }
	}

Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe » Wed Jul 11, 2007 6:06 am

it means that i would have to:
-Tokenize the text in lines
-Detect empty lines
-Rebuild each part according to these lines
....

No way to have a complete string as a separator?

Maybe a substring cutting thing manually done would be better no?

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Wed Jul 11, 2007 6:16 am

correct. another option is regular expressions.

-Max

buildere
Super wx Problem Solver
Super wx Problem Solver
Posts: 358
Joined: Thu Oct 28, 2004 3:45 pm
Location: Costa Rica

Post by buildere » Wed Jul 11, 2007 4:17 pm

Or do something like this:

Code: Select all

wxString sourceString;  // the string you want to parse
wxString token;

bool flag = true;
int index = 0;

while(flag)
{
	index = sourceString->First(wxT("\r\n\r\n"));
	if(index != wxNOT_FOUND)
	{
		token = sourceString->Left(index);
		// process token here
		sourceString = sourceString->Right(index + 5);	// shift the index 5 chars to the right	
	}
	else
		flag = false;
}
Note that I haven't tried that. The documentation says that "First" is just for legacy support and "Find" should be used instead. However, Find does not receive a string as a parameter accourding to the documentation, so I'm not sure if that will work.

Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T » Wed Jul 11, 2007 7:09 pm

Find does not receive a string as a parameter accourding to the documentation
You could use find() which takes a string parameter and is part of the std::string compatibility functions.

Sof.T
The home of Sof.T http://www.sof-t.site88.net/
Author of Programming with wxDevC++
http://sourceforge.net/projects/wxdevcpp-book/

Post Reply