java中截取下面字符串中所有的src的内容,求大神帮忙如何截取! 10
public String jiequ(String a){
int i =0;
int j=0;
try {
i = a.indexOf("src=\"");
if (i<0) {
return null;
}
j=a.indexOf("\" alt=\"\"");
String string = a.substring(i+5, j);
System.out.println(string);
a= a.substring(0,i)+a.substring(j);
System.out.println(a);
ArrayList<String> list = new ArrayList<String>();
list.add(string);
jiequ(a);//这一步有问题,但是没想到解决方法
} catch (Exception e) {
// TODO: handle exception
}
return a;
}
public static void main(String[] args) {
Stringjiequ stringjiequ = new Stringjiequ();
String a="<img src=\"/fileimage/image/20160923/20160923161211_34567.jpeg\" alt=\"\" />"
+"<img src=\"/fileimage/image/20160923/20160923161211_378222.jpeg\" alt=\"\" />";
String jiequ = stringjiequ.jiequ(a);
}
}
//一个好使,迭代器迭代不出来你自己想解决方法
可以使用正则表达式实现,我就直接上代码了:
public static void main(String[] args) {
String str = "Content=1233 <img src=\"/fileimage/image/20160923/20160923161211_378.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_1238.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_34567.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_378222.jpeg\" alt=\"\" />得得得得得其呃呃";
String regex = "(?i)src=\"([^\"]*)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
List<String> srcList = new ArrayList<>();
while(matcher.find()) {
String group = matcher.group();
srcList.add(group.replaceFirst(regex, "$1"));
}
for (String src : srcList) {
System.out.println(src);
}
}
最后得到结果如下:
/fileimage/image/20160923/20160923161211_378.jpeg
/fileimage/image/20160923/20160923161211_1238.jpeg
/fileimage/image/20160923/20160923161211_34567.jpeg
/fileimage/image/20160923/20160923161211_378222.jpeg
/fileimage/image/20160923/20160923161211_378.jpeg
/fileimage/image/20160923/20160923161211_1238.jpeg
/fileimage/image/20160923/20160923161211_34567.jpeg
/fileimage/image/20160923/20160923161211_378222.jpeg
import java.util.*;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class test {
public static void main(String args[]) {
//String xml = "<img src=\"/fileimage/image/20160923/20160923161211_378.jpeg\" alt=\"\" />";
//为了方便解析 裹了一层<data></data>
String xml = "<data><img src=\"/fileimage/image/20160923/20160923161211_378.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_1238.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_34567.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_378222.jpeg\" alt=\"\" /></data>";
try {
//SAXReader reader = new SAXReader();
Document document = DocumentHelper.parseText(xml);
//获取根节点
Element data = document.getRootElement();
//获取根节点下所有节点
List<Element> nodes = data.elements();
for (Element e : nodes) {
//获取节点下的src属性
Attribute attr = e.attribute("src");
//获取src属性下的文本值
System.out.println(attr.getValue());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
需要导入dom4j的包:
https://sourceforge.net/projects/dom4j/?source=typ_redirect
具体用法参考:
2018-03-23
package demo;
public class Demo {
public static void main(String[] args) {
String str= "Content=1233 <img src=\"/fileimage/image/20160923/20160923161211_378.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_1238.jpeg\" alt=\"\" /><img src=\"/fileimage/image/20160923/20160923161211_34567.jpeg\" alt=\"\" /><img src=\"/fil...";
String[] src = getSrc(str);
for (String string : src) {
System.out.println();
}
}
public static String[] getSrc(String str) {
StringBuffer strBuf = new StringBuffer();
String[] split = str.split("\"");
for (String string : split) {
//System.out.println(string);
if(string.contains(".jpeg")) {
strBuf.append(string).append("@");
}
}
String string = strBuf.toString();
String[] split2 = string.split("@");
return split2;
}
}
//这个行吗?能采纳吗?