Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2007
Porting and split method


Message 1 in thread

The split method in QString has changed how it deals with an empty 
string.  Personally I think the change is a mistake and it makes coding 
more difficult but even so, I thought it worth mentioning so that it 
doesn't bite other people like it did me.

With Qt3 if you did this:

	QStringList list = QStringList::split(",", "");

you would get an empty list.  There is no text to split up so you get no 
elements in the list.  If you convert this to Qt4 though:

	QStringList = QString("").split(",");

you get a list with one element in it.  To code around this in many 
places in our application, I changed the code to be like this:

	QStringList list;
	if (!text.isEmpty())
		list = text.split(",");

I liked the original behavior personally but such is life!

--
 [ signature omitted ] 

Message 2 in thread

Something weird must be the case, today I had the same problem!! (cost
good part of the afternoon to find the problem) I had not had the time
to report it. I don't know what is the best option, but I do know that
one should not change default behavior (well here the function goes from
one class to another). The actual problem is the (nice ??) feature of
"defaulting" arguments in C++ functions.

Your solution works but I think you also should look at the
possibilities of the other arguments of the split function. From the
documentation:

QStringList QString::split ( const QString & sep, SplitBehavior behavior
= KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
Splits the string into substrings wherever sep occurs, and returns the
list of those strings. If sep does not match anywhere in the string,
split() returns a single-element list containing this string.
cs specifies whether sep should be matched case sensitively or case
insensitively.
If behavior is QString::SkipEmptyParts, empty entries don't appear in
the result. By default, empty entries are kept.
Example:
     QString str = "a,,b,c";

     QStringList list1 = str.split(",");
     // list1: [ "a", "", "b", "c" ]

     QStringList list2 = str.split(",", QString::SkipEmptyParts);
     // list2: [ "a", "b", "c" ]


Albert


-----Original Message-----
From: Brad Pepers [mailto:brad@xxxxxxxxxxxxxxx] 
Sent: vrijdag 5 januari 2007 20:05
To: qt-interest@xxxxxxxxxxxxx
Subject: Porting and split method

The split method in QString has changed how it deals with an empty
string.  Personally I think the change is a mistake and it makes coding
more difficult but even so, I thought it worth mentioning so that it
doesn't bite other people like it did me.

With Qt3 if you did this:

	QStringList list = QStringList::split(",", "");

you would get an empty list.  There is no text to split up so you get no
elements in the list.  If you convert this to Qt4 though:

	QStringList = QString("").split(",");

you get a list with one element in it.  To code around this in many
places in our application, I changed the code to be like this:

	QStringList list;
	if (!text.isEmpty())
		list = text.split(",");

I liked the original behavior personally but such is life!

--
 [ signature omitted ] 

Message 3 in thread

Meer, A.J. (Albert) van der wrote:
> Something weird must be the case, today I had the same problem!! (cost
> good part of the afternoon to find the problem) I had not had the time
> to report it. I don't know what is the best option, but I do know that
> one should not change default behavior (well here the function goes from
> one class to another). The actual problem is the (nice ??) feature of
> "defaulting" arguments in C++ functions.
> 
> Your solution works but I think you also should look at the
> possibilities of the other arguments of the split function. From the
> documentation:

Yes I know I can tell it to skip empty entries but thats not what I want
here really.  I want it to ignore a blank string entirely.  I may still
want blank entries if the text is something like "foo,,bar" but also
want an empty list when the text is "" and there is no flag to give me 
this even though its how it used to work.

> QStringList QString::split ( const QString & sep, SplitBehavior behavior
> = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
> Splits the string into substrings wherever sep occurs, and returns the
> list of those strings. If sep does not match anywhere in the string,
> split() returns a single-element list containing this string.
> cs specifies whether sep should be matched case sensitively or case
> insensitively.
> If behavior is QString::SkipEmptyParts, empty entries don't appear in
> the result. By default, empty entries are kept.
> Example:
>      QString str = "a,,b,c";
> 
>      QStringList list1 = str.split(",");
>      // list1: [ "a", "", "b", "c" ]
> 
>      QStringList list2 = str.split(",", QString::SkipEmptyParts);
>      // list2: [ "a", "b", "c" ]
> 
> 
> Albert

--
 [ signature omitted ]