You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, September 25th 2011, 5:18pm

Split a QString and keep the separator

Hey QT friends,

I have a simple but nerve-racking problem:

I have a QString like this "Hello World. I am Marcus. I have a problem. QString split erases my separator. That is bad."
I want to get a QStringList with the following strings:
"Hello World."
"I am Marcus."
"I have a problem."
...

So I use the function:

Source code

1
QStringList SubStringList = verticesString.split(".",QString::KeepEmptyParts);


and I get a QStringList with these strings:
"Hello World"
"I am Marcus"
"I have a problem"
So the function QString.split erases the separator "."

I want to avoid that but I do not know, how! I want a function that splits my String into a StringList but keeps the separator in the strings.

How do I do that? Thank you for your help!

Best,
Marcus

Stiander

Beginner

  • "Stiander" is male

Posts: 41

Location: Horten

Occupation: CEO

  • Send private message

2

Monday, September 26th 2011, 7:36am

Hi,

How about something like this :

Source code

1
QString splitThis("Hello Marcus.-I am Stian.-Try this.-");QStringList splitted = splitThis.split("-");

3

Monday, September 26th 2011, 8:02am

Thanks for your answer! But the problem with that is that you are modifying the data that you read. That is not possible for me as I am trying to read out the pathes from a svg-file and therefore I can't just edit that :D I am just a little bit sad there is no option to keep the separator from splitting. That sounds weird...

Stiander

Beginner

  • "Stiander" is male

Posts: 41

Location: Horten

Occupation: CEO

  • Send private message

4

Monday, September 26th 2011, 8:11am

I see. Hmm. How about this then :

QString splitToken = ".";
QStringList splitted = splitThis.split(splitToken);
for(int i=0; i<splitted.size(); i++)
{
QString tmpStr = QString("%1%2").arg(splitted.at(i)).arg(splitToken);
splitted.replace(i,tmpStr);
}

This post has been edited 1 times, last edit by "Stiander" (Sep 26th 2011, 10:47am) with the following reason: typos


5

Monday, September 26th 2011, 7:22pm

hehee... well I thought about this, too, first. But the problem is I am parsing an SVG Path and the splitTokens are actually "M" or "m". And if you have two options, you can not replace it afterwards again, because you do not know what was there before, a "M" or an "m". Because that would have been so much work to realize that, I decided to find another way and that works pretty well for now :) But thank you anyways for the answer !!!

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

6

Monday, September 26th 2011, 7:24pm

Along with Stiander recommendations, here is one as well:

main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <QtCore>

int main()
{
    QString str = "This is a test. Another line. And yet another line. Last Line";
    QStringList list;

    // Ensure line ends with period, so we get at least one section
    if( !str.endsWith( '.' ) )
            str+='.';

    // Add str sections to list including Sep and clear space at begin/end
    for( int i = 0; i < str.count( '.' ); ++i ){
        list << str.section( '.', i, i, QString::SectionIncludeTrailingSep ).simplified();
    }

    // Output strings from string list
    foreach( QString str, list )
        qDebug() << str;
}

7

Monday, September 26th 2011, 8:42pm

dammit... well that would have worked yesterday :D Thank you! But now it is too late, I already found another way and rewrote this part of the program. But thank you very much for the answer, I will keep that into account later :)

Similar threads

Used tags

QString, separator, split