替換字符串小工具,支持大小寫是否區分
/**
* 替換字符串
* @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=\"https://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,"https://","//");
System.out.println("v="+v);
String w=replaceString(true,b,"https://","//");
System.out.println("w="+w);
最後更新:2017-04-02 16:48:17