如何从QString中删除尾部空格? [英] How do I remove trailing whitespace from a QString?

查看:1915
本文介绍了如何从QString中删除尾部空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除 QString 中所有结尾的空白字符。我想用Python函数 str.rstrip() QString



我做了一些Googling,发现这个: 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);

有更简单的方法吗?

code

有两种与修剪空格相关的方法:





如果只想删除尾部空格,您需要自己实现。这是一个模拟 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;
}


I want to remove all the trailing whitespace characters in a QString. I am looking to do what the Python function str.rstrip() with a QString.

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

So what I have right now is something like this:

while(str.endsWith( ' ' )) str.chop(1);
while(str.endsWith( '\n' )) str.chop(1);

Is there a simpler way to do this? I want to keep all the whitespace at the beginning.

解决方案

QString has two methods related to trimming whitespace:

If you want to remove only trailing whitespace, you need to implement that yourself. Here is such an implementation which mimics the implementation of 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中删除尾部空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆