简体   繁体   中英

How to replace with updated value in between string using C++?

I'm trying to replace the string with updated value. For the first time its replacing with the updated value but after that its not updating..

This is my String :

 QString   JAVASCRIPT =
                     "<script>var PAGE_ID=__PAGE_ID__; var SCROLL_PERCENTAGE=__SCROLL_PERCENTAGE__;</script>"+
                      "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/quiz_objects.js\"></script>"+
                          "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/swipe.js\"></script>"+
                          "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/NativeBridge.js\"></script>"+
                          "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/native_base.js\"></script>"+
                          "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/native_device.js\"></script>"+
                          "<script type=\"text/javascript\" language=\"javascript\" src=\"js/mobi.js\"></script>";

I want the replace the "PAGE_ID" value with updated value.

qDebug() << "currentPageInstanceeeeeeeeId" << currentPageInstanceId;
qDebug() << "javaaaaaaaaa" << JAVASCRIPT;
JAVASCRIPT.replace(__PAGE_ID__," " + currentPageInstanceId);

I'm getting the updated value in currentPageInstanceId . Here i'm replace the __PAGE_ID__ with currentPageInstanceId that's why its replaced for the first time. Now i want to replace the value between "PAGE_ID=" and ";". How can i replace. I tried like this.

qDebug() << "currentPageInstanceeeeeeeeId" << currentPageInstanceId;
    qDebug() << "javaaaaaaaaa" << JAVASCRIPT;
    Qt::CaseSensitivity cs = Qt::CaseSensitive;
    script = JAVASCRIPT.indexOf("PAGE_ID=",cs);
    script1 = JAVASCRIPT.lastIndexOf(";", cs);
    JAVASCRIPT.replace(script+""+ script1," " + currentPageInstanceId);

Thanks in Advance.

Since you want to update the value of __PAGE_ID__ (and presumably __SCROLL_PERCENTAGE__ ) several times I would approach it this way:

Set your JAVASCRIPT value to this:

QString   JAVASCRIPT =
                 "<script>var PAGE_ID=%1; var SCROLL_PERCENTAGE=%2;</script>"+
                  "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/quiz_objects.js\"></script>"+
                      "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/swipe.js\"></script>"+
                      "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/NativeBridge.js\"></script>"+
                      "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/native_base.js\"></script>"+
                      "<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/native_device.js\"></script>"+
                      "<script type=\"text/javascript\" language=\"javascript\" src=\"js/mobi.js\"></script>";

Then when you want to set the values use the arg method:

QString script = JAVASCRIPT.arg(currentPageInstanceId).arg(currentScrollPercentage);

Then use script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM