Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created tha
(char oldChar, char newChar)
| 661 | * "mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar" "the war of baronets".replace('r', 'y') returns "the way of bayonets" "sparring with a purple porpoise".replace('p', 't') returns "starring with a turtle tortoise" "JonL".replace('q', 'x') returns "JonL" (no change) |
| 662 | */ |
| 663 | public java.lang.String replace(char oldChar, char newChar){ |
| 664 | char[] buffer = value; |
| 665 | int _offset = offset; |
| 666 | int _count = count; |
| 667 | |
| 668 | int idx = _offset; |
| 669 | int last = _offset + _count; |
| 670 | boolean copied = false; |
| 671 | while (idx < last) { |
| 672 | if (buffer[idx] == oldChar) { |
| 673 | if (!copied) { |
| 674 | char[] newBuffer = new char[_count]; |
| 675 | System.arraycopy(buffer, _offset, newBuffer, 0, _count); |
| 676 | buffer = newBuffer; |
| 677 | idx -= _offset; |
| 678 | last -= _offset; |
| 679 | copied = true; |
| 680 | } |
| 681 | buffer[idx] = newChar; |
| 682 | } |
| 683 | idx++; |
| 684 | } |
| 685 | |
| 686 | return copied ? new String(buffer) : this; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. |