2

我的应用程序是基于客户端服务器的应用程序,我必须在其中显示WebView存储在SDcard.
问题是从服务器端存储的字符串就像:

"<html><img id=\"MathMLEq1\" style=\"vertical-align: middle;\" src=\"/SOCH/img.PNG\" alt=\"\"/></html>"

src tag我想在已经以编程方式定义的值之前添加一些字符,例如:

"<html><img id=\"MathMLEq1\" style=\"vertical-align: middle;\" src=\"file:///mnt/sdcard/SOCH/img.PNG\" alt=\"\"/></html>"

任何人都可以帮我解决这个问题..??
提前致谢..

4

2 回答 2

3
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 the index of occurrence of the folder name in input string, which I assume remains same
q = q.insert(separatedInd ,add); //Insert your String at that index( before start of /SOCH)
String result = q.toString();  // Contains the new final string which you require

更新代码:

正如您所评论的那样,您可能在同一个字符串中出现不止一次<img>标记,以下是用于替换所有所需位置的字符串的更新代码:

ArrayList<Integer> arrayList = new ArrayList<Integer>();
// Assuming three 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 all indexed of the occurrence of /SOCH string
for (int index = inputString.indexOf("/SOCH");
     index >= 0;
     index = inputString.indexOf("/SOCH", index + 1)){

    arrayList.add(index); //add the indexes to arrayList
}
int prev = 0;
for (int i = 0; i < arrayList.size(); i++){ // for all indexes
    q = q.insert(prev+ arrayList.get(i),add); //Insert the add string at position (index + (number of times 'add' string appears * length of 'add' string)  
    prev = (i+1)*add.length(); // calculate the next position where to insert the string

}
String result = q.toString(); //Gives the final output as desired.

希望这可以帮助

于 2013-07-18T07:13:10.767 回答
0

尝试使用JSOUP库。你可以用它来获取标签的属性,当你得到它时,你只需使用 JAVA-s 的替换方法。注意java字符串中的特殊字符(如/)。你可以把它写成这样的字符串"\""。阅读这篇关于特殊字符的文章。

于 2013-07-18T07:13:42.097 回答