Search code examples
c++qtqbytearray

QByteArray remove first 4 lines


I have QByteArray and need to remove first 4 lines. I can do it with regular expressions, for example, but is it some easier way?

UPD: first lines(more than 4) in my QByteArray is text, with '\n' in the end.


Solution

  • What about searching the fourth occurrence of '\n' (using int QByteArray::indexOf ( char ch, int from = 0 ) const) and then removing the bytes up to that position (using QByteArray & QByteArray::remove ( int pos, int len ))?

    Edit: Not tested, but something along these lines:

    QByteArray ba("first\nsecond\nthird\nfourth\nfifth");
    size_t index = 0;
    unsigned occur = 0;
    while ((index = ba.indexOf('\n', index)) >= 0){ 
        ++occur;
        if (occur == 4){
            break;
        }   
    }
    if (occur == 4){
        ba.remove(0, index + 1); 
    }