QString,删除标签和内容?

发布于 2024-09-16 17:26:02 字数 842 浏览 3 评论 0 原文

message.Text() 是一个 QString。

我想删除一些文字。

文本可以是:

  1. 正常:“这是一个文本
  2. 带标签:“something这是一个文本

首先,我发现如果文本有标签:

!message.Text().contains("<label1>", Qt::CaseInsensitive))

那么,如果有,我想删除第一部分,以获得普通文本“这是文本”。

我尝试了这个:

first=message.Text().indexOf("<label1>");
last=message.Text().lastIndexOf("</label1>");
message.Text().remove(first,last);

但我收到编译器错误 C2663。

我还知道 message.Text().remove(QChar(' 是另一种方法。但就我而言,标签之间的部分是未知的。

它可以是 somethingoisdioadj7< /代码> ....

有什么想法吗?

问候。

message.Text() is a QString.

I want to remove some text.

The text can be:

  1. Normal: "This is a text"
  2. With a label: "<label1>something</label1>This is a text"

First, I find if the text has the label:

!message.Text().contains("<label1>", Qt::CaseInsensitive))

So, if it has, I want to remove the first part, to have a normal text "This is a text".

I tried this:

first=message.Text().indexOf("<label1>");
last=message.Text().lastIndexOf("</label1>");
message.Text().remove(first,last);

But I got Compiler Error C2663.

I also know that the message.Text().remove(QChar('<label1'), Qt::CaseInsensitive); is another way to do it. But in my case, the part between the label is unkwnow.

It can be <label1>something</label1> or <label1>oisdioadj</label> or <label1>7</label1>....

Any idea?

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-09-23 17:26:02

请尝试以下操作:

#include <iostream>
using std::cout; using std::endl;
#include <QString>

int main()
{
  QString message = "<label1>something</label1>This is a test";
  const QString labelClose = "</label1>";
  const int labelCloseSize = labelClose.size();

  cout << "message: " << qPrintable(message) << endl;

  const int closePosition = message.lastIndexOf(labelClose);
  QString justText = message.remove(0, closePosition + labelCloseSize);
  cout << "just text: " << qPrintable(justText) << endl;
}

Try the following:

#include <iostream>
using std::cout; using std::endl;
#include <QString>

int main()
{
  QString message = "<label1>something</label1>This is a test";
  const QString labelClose = "</label1>";
  const int labelCloseSize = labelClose.size();

  cout << "message: " << qPrintable(message) << endl;

  const int closePosition = message.lastIndexOf(labelClose);
  QString justText = message.remove(0, closePosition + labelCloseSize);
  cout << "just text: " << qPrintable(justText) << endl;
}
千年*琉璃梦 2024-09-23 17:26:02

我的建议是:让代码中的事情保持简单,这将有助于让你的头脑变得简单。

看来您想要实现的目标与字符串更相关,而不是与标签相关。

我建议您从标签中获取文本,然后独立处理它,然后将其关联回您的标签:

QString text = message.text();

/* Do whatever you need to do here with text */

message.setText(text);

另外,您遇到的错误可能是由于您尝试直接修改 message.text() 造成的是一个const引用:显然你不能修改const的东西。

我相信您尝试实现的目标可以使用 QString::replace( )。为此,您必须使用正则表达式,因此如果您不熟悉它,这可能会很困难。

My advice here: keep things simple in your code, it will help making things simple in your head.

It seems what you want to achieve is more related to strings, than to label.

I suggest you get the text from your label, then work on it independently, then associate it back to your label:

QString text = message.text();

/* Do whatever you need to do here with text */

message.setText(text);

Also, the error you're having is probably due to the fact that you try to modify directly message.text() which is a const reference: obviously you can't modify something that is const.

I believe what you try to achieve can be done using QString::replace(). You'll have to use regular expressions for that, so if you're not familiar with it, it might be difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文