How to remove quotes and eol characters from QString?
-
I have the following code:
QString pathname = QDir::currentPath(); QString fpath = pathname + fname; QFile fs(fpath); int counter; if(!fs.open(QIODevice::ReadOnly)) { QMessageBox::information(0,"file info ",fs.errorString()); }else{ QString line = fs.readLine(); // in = your QTextStream ui->lw_lot_nbr_fd1->addItem(line); while (!line.isNull()) { line = fs.readLine(); //tempstr = QString::split('"'); if(line.startsWith(""")) { line.remove(0,1); } if(line.endsWith(""")) { line.remove(line.size()-1,1); }
The line.startsWith("""); does not evaluate to true even though there is a quote there.
the line.endsWith("""); does not evaluate to true even though there is a quote there.
If I change to line.endsWith("\r") or line.endsWith("\n") ; does not evaluate to true even though there is a return and new line there.The strings are read from a text file. Each line is a number as:
00001
00002
00003
00004 etc.When debugging and I hover over the variable "line" it says "00001\r\n
When displayed in my QListWidget the leading quote is there as is a tiny little square
A hexdump of the text file gives:
0000000 3030 3030 0d31 300a 3030 3230 0a0d 3030
0000010 3030 0d33 300a 3030 3430 0a0d 3030 3030
0000020 0d35 300a 3030 3630 0a0d 3030 3030 0d37
0000030 300a 3030 3830 0a0d 3030 3030 0d39 300a
0000040 3030 3031 0a0d 3030 3130 0d31 300a 3030
0000050 3231 0a0d 3030 3130 0d33 300a 3030 3431
0000060 0a0d 3030 3130 0d35 300a 3030 3631 0a0d
0000070 3030 3130 0d37 300a 3030 3831 0a0d 3030
0000080 3130 0d39 300a 3030 3032 0a0d 3030 3230
0000090 0d31 300a 3030 3232 0a0d 3030 3230 0d33
00000a0 300a 3030 3432 0a0d 3030 3230 0d35 300aThe text file originally came from a windows system, is this part of the problem.
Thanks for any help
emp1953
-
I would go with QString::trimmed()
Your hexdump doesn't look like a windows text file and does not match your expected strings
3030 3030 0d31 300a 3030 3230 0a0d 3030
0 0 0 0 <CR> 1 0 <LF> 0 0 2 0 <CR><LF> 0 0This does not look correct...
-
I would go with QString::trimmed()
Your hexdump doesn't look like a windows text file and does not match your expected strings
3030 3030 0d31 300a 3030 3230 0a0d 3030
0 0 0 0 <CR> 1 0 <LF> 0 0 2 0 <CR><LF> 0 0This does not look correct...
-
@emp1953 said in How to remove quotes and eol characters from QString?:
if(line.startsWith("""))
That will not even compile. You probably wanted:
if(line.startsWith("\""))
...Regards
-
@aha_1980 Interesting that you say this won't compile. From the command line it does not compile and throws an error on this line. From within the Qt Creator development environment it does in fact compile without warnings and outputs an executable. I have found that my make from the command line uses a different version of gcc than does the QtCreator development environment. An interesting turn of events.
-
@Christian-Ehrlicher The file is a column of lot numbers 00001 through 09999.
It was created in microsoft excel then highlighted and copied into notepad, saved as a .txt file.
Moved to the Linux environment. The hexdump was of this file. I implemented the trimmed() functionality which didn't change anything in my output. I'm reading each lot number into a QString line variable and putting it into a QListWidget using .AddItem.
There is still the annoying little "square" shape after every item in the QListWidget. Is that Qt trying to figure out what the \r\n is? and the square is its best guess?
How could I strip those out using a QRegExp???Thanks for everyones help
-
@emp1953 said in How to remove quotes and eol characters from QString?:
There is still the annoying little "square" shape after every item in the QListWidget.
QFile::readLine()
behaves different on Windows and Linux, your can read that in the docu.On Linux it assumes
\n
line endings, on Windows\r\n
ones, which it replaces with\n
.Your file is none of these, it has mixed endings. If you want to use it in Linux, you have to normalize the line endings first. Otherwise
readLine()
will not do what you expect and every following procedure will fail.The square bracket you see is probably the
\r
char, which cannot be displayed otherwise.From within the Qt Creator development environment it does in fact compile without warnings and outputs an executable.
But not the code you posted above. This code is invalid and no compiler will accept it. Double check what you are doing.
Regards