Posted By

pablazo on 05/16/07


Tagged

java String


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bsteadman


findAndReplace


 / Published in: Java
 

  1. protected String findAndReplace(String orginal, String toBeRemoved, String replacingStr) {
  2. StringBuffer finalStr = new StringBuffer("");
  3. int charCounter = 0;
  4. int start = 0;
  5. while (charCounter < orginal.length()) {
  6. start = orginal.indexOf(toBeRemoved, charCounter);
  7. if (start >= 0) {
  8. finalStr.append(orginal.substring(charCounter, start));
  9. charCounter = start + toBeRemoved.length();
  10. finalStr.append(replacingStr);
  11. } else {
  12. finalStr.append(orginal.substring(charCounter, orginal.length()));
  13. return finalStr.toString();
  14. }
  15. }
  16. return finalStr.toString();
  17. }

Report this snippet  

You need to login to post a comment.