Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete new-line character in asctime in QString

Tags:

c++

qt

qstring

I am converting time_t to human readable format. For this purpose I am using asctime() function. However it is also mentioned in c++ references.

The string is followed by a new-line character ('\n') and terminated with a null-character.

I know that I can delete '\n' character if I use char pointer. Such as;

char * str; 
str(strlen(str)-1) = '\0'; // repleacing new-line char to null-char

However how can I delete new-line characther in QString? Here is my example;

    time_t rawtime;
    struct tm * timeinfo;
    QString strList;
    rawtime = 1430687052;
    timeinfo = localtime (&rawtime);
    strList += asctime(timeinfo);

EDIT:

I am creating report file, so that I have lots of '\n' char in my QString, thats why replacing all '\n' char to null pointer is not good idea.

like image 325
goGud Avatar asked Dec 09 '22 03:12

goGud


1 Answers

I would use QString::trimmed() function to remove all trailing and leading whitespace characters, such as '\t', '\n', '\v', '\f', '\r', and ' '. I.e.:

strList = strList.trimmed();
like image 187
vahancho Avatar answered Dec 26 '22 04:12

vahancho