message.Text() 是一个 QString。
我想删除一些文字。
文本可以是:
- 正常:“
这是一个文本
”
- 带标签:“
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(' 是另一种方法。但就我而言,标签之间的部分是未知的。
它可以是 something
或 oisdioadj
或 7< /代码> ....
有什么想法吗?
问候。
message.Text() is a QString.
I want to remove some text.
The text can be:
- Normal: "
This is a text
"
- 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.
发布评论
评论(2)
请尝试以下操作:
Try the following:
我的建议是:让代码中的事情保持简单,这将有助于让你的头脑变得简单。
看来您想要实现的目标与字符串更相关,而不是与标签相关。
我建议您从标签中获取文本,然后独立处理它,然后将其关联回您的标签:
另外,您遇到的错误可能是由于您尝试直接修改 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:
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.