代碼如下:
import java
public class regex {
public regex() {
}
public static String replaceByRegex (String input
StringBuffer sb = null;
if (input==null||input
if (toChg==null||toChg
Pattern chagePattern = pile(toChg);
Matcher inputMatcher = chagePattern
sb = new StringBuffer();
while (inputMatcher
inputMatcher
}
inputMatcher
return sb
}
public static String replaceByStringBuffer(String _old
{
if (_old==null) {return null;}
StringBuffer _temp=new StringBuffer();
int i=
int j=
while((j=_old
{
_temp
_old=_old
}
_temp
return _temp
}
public static String replaceByString(String _old
{
if (_old==null) {return null;}
String _temp=
int i=
int j=
while((j=_old
{
_temp+=_old
_old=_old
}
_temp+=_old;
return _temp;
}
public static void main(String[] args) throws Throwable {
String tmp =
long t = System
for (int i=
System
t = System
for (int i=
System
t = System
for (int i=
System
}
}
測試結果:
replace it by string buffer :
replace it by string :
replace it by regex :
可見
其中subString 也會降低處理的性能
不過在一般數據量不大
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19241.html