Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 46
Registered: ‎03-07-2014
My Device: BlackBerryZ10
My Carrier: BlackBerry

How to replace the new values in the string

Hi

 

I'm trying to replace the current string value with updated value but its not replacing with updated value. Its replacing only for the first time after its not replacing with any updated value.

QString JAVASCRIPT = "<script>var PAGE_ID=__PAGE_ID__; var SCROLL_PERCENTAGE=__SCROLL_PERCENTAGE__;</script>";

// data contains webview content.I'm getting the updated value in //"currentPageInstanceId". I'm replacing with the updated values in //getJavaScript()

data = data.replace("</head>", "\n" + getJavaScript() + "\n" + "</head>");

QString MinervaLatest::getJavaScript()
{
	QString script;
	 qDebug() << "currentPageInstanceeeeeeeeId" << currentPageInstanceId;
	 qDebug() << "javaaaaaaaaa" << JAVASCRIPT;
	 script = JAVASCRIPT.replace("__PAGE_ID__"," " + currentPageInstanceId);
	 qDebug() << "replcae javaaaa" << JAVASCRIPT;

	//qDebug()<< "scripttttttt" << script;
	return script;

}


 

Thanks in Advance.

Highlighted
Retired
Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: How to replace the new values in the string

You probably want to take a look at the reference for QString::replace() and make sure you're using it in the way you expected:

 

http://qt-project.org/doc/qt-4.8/qstring.html#replace-8

 

 QString myString = "Blah Blah XXX Blah Blah";
 myString.replace(QString("XXX"), QString("Wibble"));
// results in myString being changed to => "Blah Blah Wibble Blah Blah"

You seem to be using it as:

 

 QString myString = "Blah Blah XXX Blah Blah";
 QString myOtherString;
myOtherString = myString.replace(QString("XXX"), QString("Wibble"));
// results in myString being changed to => "Blah Blah Wibble Blah Blah"
// as well as myOtherString being set to => "Blah Blah Wibble Blah Blah"

Is this what you intended?

 

The reason QString::replace() returns a QString & is so that it's easy to chain things like:

 

 QString myString = "Blah Blah XXX Blah Blah YYY";

 myString.replace(QString("XXX"), QString("Wibble")).replace(QString("YYY"), QString("Frob"));
// results in myString being changed to => "Blah Blah Wibble Blah Blah Frob"