替换字符串中部分字符串,java

来源:互联网 发布:央视市场研究 知乎 编辑:程序博客网 时间:2024/06/02 13:12
[java] view plain copy
  1. public  String replace(String from, String to, String source)   
  2.      {     
  3.         if (source == null || from == null || to == null)     
  4.             return null;     
  5.         StringBuffer bf = new StringBuffer("");     
  6.         int index = -1;     
  7.         while ((index = source.indexOf(from)) != -1)   
  8.         {     
  9.             bf.append(source.substring(0, index) + to);     
  10.             source = source.substring(index + from.length());     
  11.             index = source.indexOf(from);     
  12.         }     
  13.         bf.append(source);     
  14.         return bf.toString();     
  15.     }  


http://slddyb.blog.51cto.com/2294765/409676

0 0
原创粉丝点击