/**
* 替换字符串
* @param isCase 是否区分大小写
* @param source 源字符串
* @param oldstring 待替换旧字符串
* @param newstring 新字符串
* @return 替换后的字符串
*/
public static String replaceString(Boolean isCase,String source, String oldstring,String newstring){
String ret="";
if (!isCase) //不区分大小写
{
Matcher m = Pattern.compile(oldstring, Pattern.CASE_INSENSITIVE).matcher(source);
ret=m.replaceAll(newstring);
//System.out.println("使用正则表达式不区分大小写的替换结果"+ret);
}
else // 区分大小写
{
Matcher m = Pattern.compile(oldstring, Pattern.CANON_EQ).matcher(source);
ret=m.replaceAll(newstring);
//System.out.println("使用正则表达式区分大小写的替换结果"+ret);
}
return ret;
}
测试:
String b="<img src=\"http://www.aaa.com/\">";
b+="<img src=\"hTTp://www.bbb.com/\">";
b+="<img src=\"HTTP://www.ccc.com/\">";
b+="<img src=\"hTTP://www.ddd.com/\">";
String v=replaceString(false,b,"http://","//");
System.out.println("v="+v);
String w=replaceString(true,b,"http://","//");
System.out.println("w="+w);