当前页面: 开发资料首页 → J2SE 专题 → 各位老师帮忙,一个String基本使用的问题
各位老师帮忙,一个String基本使用的问题
摘要: 各位老师帮忙,一个String基本使用的问题
String img="ghy
![]()
adf
![]()
";
img字符串中有若干个
![]()
我想在每个
![]()
前面加上123怎么办??感谢
img=img.replaceAll("
![]()
","123
![]()
");
StringBuffer sb=new StringBuffer(img);
可以试试StringBuffer 的indexOf和lastIndexOf("
![]()
")定位一下,然后插入,可能效率不高,死办法
1楼的办法好
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 = "";
StringBuffer endimg = new StringBuffer(img);
int temp = 0;
int pos = 0;
// / System.out.print(img.indexOf("
![]()
while (img.indexOf("
![]()
pos = img.indexOf("
![]()
String srcstring = getSRC(img, pos);
System.out.println(srcstring);
String link = "";
endimg.insert(pos + temp, "+ "/" target=/"_blank/">");
temp = temp + link.length();
pos = pos + 4;
System.out.println(endimg);
}
}
}
调试成功了,比较麻烦,,就是遍历,,感谢一楼,但不是我想要的功能
是把图像转换成链接啊
问题和后面楼主自己贴的结果根本不是一个意思