J2SE1.4里的String.replaceAll效率好像不高丫,大家认为呢?

iStringTheory 2003-10-20 02:28:39
有没有效率高的字符串替换函数?
...全文
155 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbhyk 2003-10-20
  • 打赏
  • 举报
回复
看我的:
public static String replace(String s, String oldString, String newString)
{
if(s == null)
throw new NullPointerException("source String cannot be null");
if(oldString == null)
throw new NullPointerException("old String cannot be null");
if(newString == null)
throw new NullPointerException("new String cannot be null");
if(oldString.length() == 0)
throw new IllegalArgumentException("old String cannot be empty");

StringBuffer result = new StringBuffer();

int start = 0;
int end = s.indexOf(oldString);
while(end != -1)
{
result.append(s.substring(start, end));
result.append(newString);
start = end + oldString.length();
end = s.indexOf(oldString, start);
}
result.append(s.substring(start, s.length()));

return result.toString();
}
iStringTheory 2003-10-20
  • 打赏
  • 举报
回复
zez(思恩 为老婆多挣钱 鹤清风) ( ) :
用JProfiler测的?
zez 2003-10-20
  • 打赏
  • 举报
回复
我自己写的速度是它的40% :)


------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧