各位老师帮忙,一个String基本使用的问题 String img="ghy<img>adf<img>";img字符串中有若干个<img>我想在每个<img>前面加上123怎么办??感谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 img=img.replaceAll("<img>","123<img>"); StringBuffer sb=new StringBuffer(img);可以试试StringBuffer 的indexOf和lastIndexOf("<img>")定位一下,然后插入,可能效率不高,死办法 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); } }}调试成功了,比较麻烦,,就是遍历,,感谢一楼,但不是我想要的功能 请求arraylist里面的分段问题。 二维数组赋值问题异常 再问SVN的知识点 关于DefaultListModel 基础概念问题 java 高手帮忙??? 遇到简单问题了啊,可能比较常见啊。 如何实现在SWING窗体内打开PDF Jedeveloper用一段就没有新建菜单了,what's wrong? 请问用jet把jar文件转成exe问题。。。。。我遇到找不到莫块问题。。。。。 请教JAVA包的问题? HQL语句中,如何select后面加入一个对象。如:select new String(),cat.name from Cat as cat
可以试试StringBuffer 的indexOf和lastIndexOf("<img>")定位一下,然后插入,可能效率不高,死办法
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);
}
}}
调试成功了,比较麻烦,,就是遍历,,感谢一楼,但不是我想要的功能