String img="ghy<img>adf<img>";img字符串中有若干个<img>我想在每个<img>前面加上123怎么办??感谢

解决方案 »

  1.   

    img=img.replaceAll("<img>","123<img>");
      

  2.   

    StringBuffer sb=new StringBuffer(img);
    可以试试StringBuffer 的indexOf和lastIndexOf("<img>")定位一下,然后插入,可能效率不高,死办法
      

  3.   

    public class img { public static String getSRC(String img, int pos) {
    String src = "";
    int srcpos = img.indexOf("src", pos);
    System.out.println("src的位置" + srcpos);
    int tagbeginpos = img.indexOf("\"", srcpos);
    System.out.println("\"的开始位置" + tagbeginpos);
    int tagendpos = img.indexOf("\"", tagbeginpos + 1);
    System.out.println("\"的结束位置" + tagendpos);
    src = img.substring(tagbeginpos + 1, tagendpos);
    System.out.println("SRC的内容" + src);
    return src;
    } public static void main(String[] args) {
    // TODO Auto-generated method stub
    String img = "<img src=\"123.jpg\" width=\"1764\" height=\"1326\" border=\"0\"><img src=\"456.jpg\" width=\"1764\" height=\"1326\" border=\"0\">";
    StringBuffer endimg = new StringBuffer(img);
    int temp = 0;
    int pos = 0;
    // / System.out.print(img.indexOf("<img", 7));
    while (img.indexOf("<img", pos) != -1) { pos = img.indexOf("<img", pos);
    String srcstring = getSRC(img, pos);
    System.out.println(srcstring);
    String link = "<a href=\"" + srcstring + "\" target=\"_blank\">";
    endimg.insert(pos + temp, "<a href=\"" + srcstring
    + "\" target=\"_blank\">");
    temp = temp + link.length();
    pos = pos + 4;
    System.out.println(endimg);
    }
    }}
    调试成功了,比较麻烦,,就是遍历,,感谢一楼,但不是我想要的功能