Replaces every occurrence of find in s with replace .
(
String s,
String find,
String replace)
| 616 | * <code>replace</code>. |
| 617 | */ |
| 618 | public static String replace( |
| 619 | String s, |
| 620 | String find, |
| 621 | String replace) { |
| 622 | // let's be optimistic |
| 623 | int found = s.indexOf(find); |
| 624 | if (found == -1) { |
| 625 | return s; |
| 626 | } |
| 627 | StringBuilder sb = new StringBuilder(s.length()); |
| 628 | int start = 0; |
| 629 | for (;;) { |
| 630 | for (; start < found; start++) { |
| 631 | sb.append(s.charAt(start)); |
| 632 | } |
| 633 | if (found == s.length()) { |
| 634 | break; |
| 635 | } |
| 636 | sb.append(replace); |
| 637 | start += find.length(); |
| 638 | found = s.indexOf(find, start); |
| 639 | if (found == -1) { |
| 640 | found = s.length(); |
| 641 | } |
| 642 | } |
| 643 | return sb.toString(); |
| 644 | } |
| 645 | |
| 646 | /** Right-pads a string with {@code padChar} until it reaches {@code size}. |
| 647 | * |