7
QString line = "example string";

Now I want to erase the space between 'example' and 'string' so that I get a string like this "examplestring". Is there a function in Qt which erases a character under the given index or should I write this function myself ?

samvel1024
  • 1,123
  • 4
  • 15
  • 39
  • Did you look at the [QString reference](http://qt-project.org/doc/qt-5.0/qtcore/qstring.html)? – Oswald Oct 18 '13 at 07:58

3 Answers3

10

What about QString::remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) function? You can use ' ' as a first argument. I.e.:

QString line = "example string";
line.remove(' ');
vahancho
  • 20,808
  • 3
  • 47
  • 55
8
line = line.remove(index,1);

see the documentation

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
0

You can use

line.replace(QString(" "), QString(""));
zozermania
  • 85
  • 1
  • 6
  • Yes , but as far as I understand it will replace all the " "-s with "" , but what I want is to erase only one space. – samvel1024 Oct 18 '13 at 08:02
  • 1
    That is correct. In that case, you can use @ratchet-freak 's method, you just replace `index` with `8`, this is the index where your space is located inside the string. – zozermania Oct 18 '13 at 08:08