13

How to remove /Job from /home/admin/job0/Job

QString name = "/home/admin/job0/Job"

I want to remove last string after"/"

demonplus
  • 5,613
  • 12
  • 49
  • 68
Sijith
  • 3,740
  • 17
  • 61
  • 101
  • 1
    Since you seem to be playing with directories, you can use `QDir` class and its [`QDir::cdUp`](http://doc.qt.io/qt-5/qdir.html#cdUp) method. – thuga Nov 09 '17 at 12:22
  • Is this a qt question, or a C++ question? What have you tried so far? Do you want to remove just the _last string after "/"_ or _remove /Job_ including the slash? – Raimund Krämer Nov 09 '17 at 12:37
  • Maybe you can use `QFileInfo` for this task. – Aleph0 Nov 09 '17 at 13:14
  • `QDir::cdUp` won't work if the parent directory does not exist. Better to use `QFileInfo(name).dir().path()`, which doesn't check for existence. – ekhumoro Nov 09 '17 at 19:31

6 Answers6

23

You have QString::chop() for the case when you already know how many characters to remove.
It is same as QString::remove(), just works from the back of string.

mekkanizer
  • 722
  • 2
  • 9
  • 28
15

Find last slash with QString::lastIndexOf. After that get substring with QString::left till the position of the last slash occurrence

QString name = "/home/admin/job0/Job";
int pos = name.lastIndexOf(QChar('/'));
qDebug() << name.left(pos);

This will print:

"/home/admin/job0"

You should check int pos for -1 to be sure the slash was found at all.

To include last slash in output add +1 to the founded position

qDebug() << name.left(pos+1);

Will output:

"/home/admin/job0/"
Xplatforms
  • 2,102
  • 17
  • 26
5

Maybe easiest to understand for later readers would probably be:

QString s("/home/admin/job0/Job");
s.truncate(s.lastIndexOf(QChar('/'));
qDebug() << s;

as the code literaly says what you intended.

transistor
  • 649
  • 6
  • 11
  • there's one problem in some cases: If the string contains no slash, it is cleared completely – xeruf Jan 02 '21 at 12:30
1

You can do something like this:

QString s("/home/admin/job0/Job");
s.remove(QRegularExpression("\\/(?:.(?!\\/))+$"));
// s is "/home/admin/job0" now
vahancho
  • 20,808
  • 3
  • 47
  • 55
0

If you are using Qt upper than 6 and sure that "/" constains in your word you should use QString::first(qsizetype n) const function instead QString::left(qsizetype n) const

Example:

QString url= "/home/admin/job0/Job"
QString result=url.first(lastIndexOf(QChar('/')));

If you run these code:

QElapsedTimer timer;
timer.start();
for (int j=0; j<10000000; j++)
{
    QString name = "/home/admin/job0/Job";
    int pos = name.lastIndexOf("/");
    name.left(pos);
}
qDebug() << "left method" << timer.elapsed() << "milliseconds";
timer.start();
for (int j=0; j<10000000; j++)
{
    QString name = "/home/admin/job0/Job";
    int pos = name.lastIndexOf(QChar('/'));
    name.first(pos);
}
qDebug() << "frist method" << timer.elapsed() << "milliseconds";

Results:

left method 10034 milliseconds

frist method 8098 milliseconds

-2

sorry for replying to this post after 4 years, but I have (I think) the most efficient answer. You can use

qstr.remove(0, 1); //removes the first character
qstr.remove(1, 1); //removes the last character

Thats everything you have to do, to delete characters ONE BY ONE (first or last) from a QString, until 1 character remains.

  • 1
    How does this solves the problem of the OP, he wants the end of the string, not the beginning. – m7913d Sep 26 '21 at 21:11
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '21 at 23:13