java - How to Replace src Attribute of <img> Tag from String in Android -
my application client-server based application in have display images in webview
stored in sdcard
.
problem string stored server end like:
"<html><img id=\"mathmleq1\" style=\"vertical-align: middle;\" src=\"/soch/img.png\" alt=\"\"/></html>"
i want add characters before value defined in src tag
programmatically like:
"<html><img id=\"mathmleq1\" style=\"vertical-align: middle;\" src=\"file:///mnt/sdcard/soch/img.png\" alt=\"\"/></html>"
can me through this..??
in advance..
string inputstring = "<html><img id=\"mathmleq1\" style=\"vertical-align: middle;\" src=\"/soch/img.png\" alt=\"\"/></html>"; stringbuffer q= new stringbuffer(inputstring); string add = "file:///mnt/sdcard"; int separatedind = inputstring.indexof("/soch"); //get index of occurrence of folder name in input string, assume remains same q = q.insert(separatedind ,add); //insert string @ index( before start of /soch) string result = q.tostring(); // contains new final string require
updated code:
as commented may have more 1 occurrence of <img>
tag in same string, here updated code replace strings @ desired places:
arraylist<integer> arraylist = new arraylist<integer>(); // assuming 3 times here string inputstring = "<html><img id=\"mathmleq1\" style=\"vertical-align: middle;\" src=\"/soch/img.png\" alt=\"\"/></html> <html><img id=\"mathmleq1\" style=\"vertical-align: middle;\" src=\"/soch/img.png\" alt=\"\"/></html> <html><img id=\"mathmleq1\" style=\"vertical-align: middle;\" src=\"/soch/img.png\" alt=\"\"/></html>"; stringbuffer q= new stringbuffer(inputstring); string add = "file:///mnt/sdcard"; //get indexed of occurrence of /soch string (int index = inputstring.indexof("/soch"); index >= 0; index = inputstring.indexof("/soch", index + 1)){ arraylist.add(index); //add indexes arraylist } int prev = 0; (int = 0; < arraylist.size(); i++){ // indexes q = q.insert(prev+ arraylist.get(i),add); //insert add string @ position (index + (number of times 'add' string appears * length of 'add' string) prev = (i+1)*add.length(); // calculate next position insert string } string result = q.tostring(); //gives final output desired.
hope helps
Comments
Post a Comment