我编写了一个方法,该方法将html
代码存储为字符串,并将此html
代码中的图像与我选择的图像交换。我经常遇到String index out of range: -1
异常。我不明白我在哪里可能会越界。有人可以看看我的代码并给我一个提示吗?
public static String replaceImgs(String htmlCode){
String finalString = "";
String toFind = "img src=\"";
String imgReplace = "img src=\"http://media4.popsugar-assets.com/files/2014/08/08/878/n/1922507/caef16ec354ca23b_thumb_temp_cover_file32304521407524949.xxxlarge/i/Funny-Cat-GIFs.jpg";
String tagReplace = "\">";
String rest = "";
int index = htmlCode.indexOf(toFind);
String firstString = htmlCode.substring(0, index);
rest = htmlCode.substring(index+1, htmlCode.length());
index = rest.indexOf(tagReplace);
String secondString = rest.substring(index, rest.length());
finalString = firstString.concat(imgReplace).concat(secondString);
return finalString;
}