Copies this string replacing occurrences of the specified character with another character. @param oldChar the character to replace. @param newChar the replacement character. @return a new string with occurrences of oldChar replaced by newChar.
(char oldChar, char newChar)
| 1289 | * @return a new string with occurrences of oldChar replaced by newChar. |
| 1290 | */ |
| 1291 | public String replace(char oldChar, char newChar) { |
| 1292 | int index = indexOf(oldChar, 0); |
| 1293 | if (index == -1) { |
| 1294 | return this; |
| 1295 | } |
| 1296 | |
| 1297 | char[] buffer = new char[count]; |
| 1298 | System.arraycopy(value, offset, buffer, 0, count); |
| 1299 | do { |
| 1300 | buffer[index++] = newChar; |
| 1301 | } while ((index = indexOf(oldChar, index)) != -1); |
| 1302 | return new String(0, count, buffer); |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Copies this string replacing occurrences of the specified target sequence |