セグメント解析文字列、置換指定文字の検索
2654 ワード
public class ReplaceUrl {
static String s = "<DIV style='FONT-SIZE: 12px; MARGIN: 0px auto; COLOR: #666; HEIGHT: auto; TEXT-ALIGN: center'><a style='COLOR: #666' href='#'>Home page</a> - <a style='COLOR: #666' href='#'>Add to Favorites</a> - <a style='COLOR: #666' href='#'>Stock</a> - <a style='COLOR: #666' href='#'>China Brand</a> - <a style='COLOR: #666' href='#'>My Office</a> - <a style='COLOR: #666' href='#'>About Us</a> - <a style='COLOR: #666' href='#'>Contact Us</a> - <a style='COLOR: #666' href='#'>Sitemap</a> - <a style='COLOR: #666' href='#'>Help</a><BR>Copyright Notice ? 2009 My Global-inventory.cc All rights reserved.</DIV>";
static String resultString = "";//
static String lastString = s;//
static String url = "http://www.java001.cn/";
/**
*
* @param s
* @return
*/
public static String convert(String s,String regx){
while(lastString.indexOf("href='") != -1 || lastString.indexOf("href=\"") != -1){
String bufferS = "";//
String head = "";//
int pos = lastString.indexOf("href='");
int pos2 = lastString.indexOf("href=\"");
if(pos == -1 && pos2 != -1){
pos = pos2;
}
int endpos = lastString.indexOf("</a>");
head = lastString.substring(0,pos);
bufferS = lastString.substring(pos,endpos+4);
bufferS = bufferS.replaceAll("href='", "href='"+regx);
if(pos == -1 && pos2 != -1){
bufferS = bufferS.replaceAll("href=\"", "href=\""+regx);
}
resultString += head+bufferS;
lastString = lastString.substring(endpos+4,lastString.length());
//System.out.println("head:"+head);
//System.out.println("bufferS:"+bufferS);
//System.out.println("resultS:"+resultS);
//System.out.println("bufferS2:"+bufferS2);
//System.out.println("======================================================================");
}
return resultString;
//System.out.println("resultS:"+resultString);
}
}