我的空间,写我所写,禅我所藏

与我一起遨游吧

 

替换字符串的方法

 /**
  * Replace all occurence of forReplace with replaceWith in input string.
  *
  * @param input
  *            represents input string
  * @param forReplace
  *            represents substring for replace
  * @param replaceWith
  *            represents replaced string value
  * @return new string with replaced values
  */
 private static String replaceAll(String input, String forReplace,
   String replaceWith) {
  if (input == null)
   return null;
  StringBuffer result = new StringBuffer();
  boolean hasMore = true;
  while (hasMore) {
   int start = input.indexOf(forReplace);
   int end = start + forReplace.length();
   if (start != -1) {
    result.append(input.substring(0, start) + replaceWith);
    input = input.substring(end);
   } else {
    hasMore = false;
    result.append(input);
   }
  }
  if (result.toString().equals(""))
   return input; // nothing is changed
  else
   return result.toString();
 }

posted on 2007-06-25 10:59 imcb 阅读(159) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜