【问题标题】:Qt5.5 QByteArray indexOf mid wrong resultQt5.5 QByteArray indexOf mid 错误结果
【发布时间】:2016-08-07 02:27:03
【问题描述】:

我在 QByteArray 中有一个 XML 文件我正在使用 indexOf 方法在数组中查找一个字符串,但返回的位置不正确。如果我使用 qDebug 检查数据内容,我可以看到数据有转义字符,这不是问题,但我不认为 indexOf 正在计算转义字符。

例如结果来自:

    qDebug() << arybytXML;

由此产生的一个 sn-p 是:

    <?xml version="1.0" encoding="utf-8"?><!--\n Node:          gui\n Attrbuttes:    left, right, top and bottom defines the pixel white space to allow\n                from the edge of the display\n\t\tlanguage, should be set to the appropriate country code, an XML file named using\n\t\tthe country code must exist, e.g. 44.xml\n//-->\n<gui id=\"root\" bottom=\"0\" left=\"0\" right=\"0\" top=\"24\" language=\"44\">

我使用代码:

    intOpenComment = arybytXML.indexOf("<!--");

结果是 intOpenComment 是 39。如果我然后搜索结束评论并尝试提取数据,我得到错误的结果:

    intClosingComment = arybytXML.indexOf("-->", intOpenComment);

    QString strComment = arybytXML.mid(intOpenComment
                                      ,intClosingComment + strlen("-->"));

结果:

    <!--\n Node:          gui\n Attrbuttes:    left, right, top and bottom defines the pixel white space to allow\n                from the edge of the display\n\t\tlanguage, should be set to the appropriate country code, an XML file named using\n\t\tthe country code must exist, e.g. 44.xml\n//-->\n<gui id=\"root\" bottom=\"0\" left=\"0\" rig"

结果应该在-->之后停止,为什么还有更多数据?

【问题讨论】:

    标签: substring indexof qt5.5 qbytearray


    【解决方案1】:

    问题是使用mid时,第二个参数应该是字节数,需要去掉'intOpenComment'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多