English 中文(简体)
我怎么能把拖 trail的白色空间从摇篮中去除?
原标题:How do I remove trailing whitespace from a QString?

我想删除<条形码>QString中所有线索白空间特征。 我期待着以<代码>QString的<<>str.rstrip(>。

I did some Googling, and found this: http://www.qtforum.org/article/20798/how-to-strip-trailing-whitespace-from-qstring.html

因此,我现在有权这样做:

while(str.endsWith(     )) str.chop(1);
while(str.endsWith(  
  )) str.chop(1);

是否有更简单的方法这样做? 我想从一开始就保留所有的白色空间。

最佳回答

<代码>QString有两个方法与三明白空间有关:

如果你只想拆除破碎的白色空间,那么你需要执行这一天。 此处是实施<代码>trimmed:

QString rstrip(const QString& str) {
  int n = str.size() - 1;
  for (; n >= 0; --n) {
    if (!str.at(n).isSpace()) {
      return str.left(n + 1);
    }
  }
  return "";
}
问题回答

QString provides only two trimming-related functions. In case if they don t suit your needs, I m afraid you need to implement your own custom trimming function.

QString QString::simplified () const
Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.

QString str = "  lots	 of
whitespace
 ";
str = str.simplified();
// str == "lots of whitespace";

QString QString::trimmed () const
Returns a string that has whitespace removed from the start and the end.

QString str = "  lots	 of
whitespace
 ";
str = str.trimmed();
// str == "lots	 of
whitespace"

You can do it with a regexp:

#include <QtCore>

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);

    QString str("Hello world    ");

    qDebug() << str;

    str.remove(QRegExp("\s+$"));

    qDebug() << str;

    return 0;
}

Whether this would be faster, I m not sure.

如果你不希望提供大体的复印件:

QString & rtrim( QString & str ) {
  while( str.size() > 0 && str.at( str.size() - 1 ).isSpace() )
    str.chop( 1 );
  return str;
}

QString::Trimmed() removes whitespace from the start and the end - if you are sure there is no whitespace at the start you can use this.

1. 没有深度拷贝,也没有一再要求大小/秒:

QString & rtrimInPlace (QString &str) {
    for (int n = str.size() - 1; n >= 0; -- n)
        if (!str.at(n).isSpace()) {
            str.truncate(n + 1);
            break;
        }
    return str;
}

这是Frank S. Thomas的回答的一种变化:

QString rstrip(const QString& str, const char *skip = " 
	") {
  int n = str.size() - 1;
  for (; n >= 0; --n) {
    if (0 == strchr(skip, str.at(n).toAscii()))
      return str.left(n + 1);
  }
  return "";
}

如我所知,唯一正在运行的系统是trimmed(<>/code>和simplated(>)。 因此,你将需要人工裁量。 在这种情况下,您应使用<代码>QChar的功能isspace()来检查特性是否具有空间特性。

你们可以增加一个非空间的果园,然后拆除地皮。

str1=QString("-"+str).trimmed().mid(1);




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签