Page 1 of 1

Another wxString split function

Posted: Fri Aug 12, 2005 9:52 am
by ssigala
YASF (Yet another split function...): :wink:

Code: Select all

// prototype

bool SplitString(const wxString& str, char sep, int nofields, bool trim,
     wxString* field1, wxString* field2, wxString* field3=0, wxString* field4=0, wxString* field5=0,
     wxString* field6=0, wxString* field7=0, wxString* field8=0, wxString* field9=0, wxString* field10=0);

// examples:
//
//     wxString first, second, third;
//     SplitString("foo:bar:baz", ':', 3, false, &first, &second, &third);
//     SplitString("foo : bar: baz", ':', 3, true, &first, &second, &third);

bool SplitString(const wxString& str, char sep, int nofields, bool trim,
     wxString* field1, wxString* field2, wxString* field3, wxString* field4, wxString* field5,
     wxString* field6, wxString* field7, wxString* field8, wxString* field9, wxString* field10)
{
	wxString s = str;
	wxString* fields[10] = {field1, field2, field3, field4, field5, field6, field7, field8, field9, field10};
	int count=0;
	while (nofields >= 2) {
		int pos = s.Find(sep);
		if (pos == -1)
			return false;
		wxASSERT(fields[count]);
		if (fields[count]) *fields[count] = (trim ? s.Left(pos).Trim().Trim(false) : s.Left(pos));
		s = s.Mid(pos+1);
		++count;
		--nofields;
	}
	wxASSERT(fields[count]);
	if (fields[count]) *fields[count] = (trim ? s.Trim().Trim(false) : s);
	return true;
}

Posted: Fri Aug 12, 2005 10:51 am
by AkiraDev
Hi!

This piece of code can nicely be used in home-made parsers... although there is something I don't quite understand.
Why use 10 pointers to wxStrings as argument, instead of just passing a reference to a (resisable) wxArrayString - which extends the maximum number of extractable tokens from 10 to the available memory?

Posted: Fri Aug 12, 2005 11:03 am
by ssigala
AkiraDev wrote: This piece of code can nicely be used in home-made parsers... although there is something I don't quite understand.
Why use 10 pointers to wxStrings as argument, instead of just passing a reference to a (resisable) wxArrayString - which extends the maximum number of extractable tokens from 10 to the available memory?
I agree with you, using a wxArrayString is better and more general, for a string split function.

Actually I have written the SplitString function for splitting fields in configuration-like files:

Code: Select all

color = red
title = "hello world"
foo = 1235
The usage is really simple and compact:

Code: Select all

wxString line;
// ... read line
wxString variable, value;
SplitString(line, '=', 2, true, &variable, &value);

Posted: Fri Aug 12, 2005 12:37 pm
by Jorg
It should already be part of wxWidgets because it is that handy!

- Jorgen

Posted: Thu Dec 08, 2005 4:22 pm
by go_micha
for same purpose I use the next very simple function:

Code: Select all

// returns false if separator was not found. In this case rLeft contains input string
bool SplitToken(const wxString& input, const wxChar& sep, wxString& rLeft, wxString& rRight)
{
  rLeft = input.BeforeFirst(sep);
  if (rLeft == input)
    return false;
  rRight= input.AfterFirst(sep);
  if (rRight.IsEmpty())
    return false;

  return true;
}
Usage:
for pairs like this:
tag=123
param=Abc
...

Code: Select all

wxString sValue("tag=123");
wxString sLeft, sRight;
SplitToken(sValue, "=", sLeft, sRight);

Posted: Sat Jan 28, 2006 1:48 pm
by Takeshi Miya
I just use wxStringTokenizer :)