Add removeRange method.

Breaking change in replaceRange(IntRange, String) method: the end index of the range now is included in the part being replaced.
This commit is contained in:
Ilya Gorbunov
2015-03-19 15:38:24 +03:00
parent ba15e5b295
commit 4c58f4ec20
2 changed files with 47 additions and 8 deletions
+28 -7
View File
@@ -308,17 +308,38 @@ public fun String.replaceRange(firstIndex: Int, lastIndex: Int, replacement: Str
/**
* Replace the part of string at the given [range] with the [replacement] string.
*
* The end index of the [range] is included in the part to be replaced.
*/
public fun String.replaceRange(range: IntRange, replacement: String): String {
if (range.end < range.start)
throw IndexOutOfBoundsException("Last index (${range.start}) is less than first index (${range.end})")
val sb = StringBuilder()
sb.append(this, 0, range.start)
sb.append(replacement)
sb.append(this, range.end, length)
public fun String.replaceRange(range: IntRange, replacement: String): String = replaceRange(range.start, range.end + 1, replacement)
/**
* Removes the part of a string at a given range.
* @param firstIndex the index of the first character to be removed.
* @param lastIndex the index of the first character after the removed part to keep in the string.
*
* [lastIndex] is not included in the removed part.
*/
public fun String.removeRange(firstIndex: Int, lastIndex: Int): String {
if (lastIndex < firstIndex)
throw IndexOutOfBoundsException("Last index ($lastIndex) is less than first index ($firstIndex)")
if (lastIndex == firstIndex)
return this
val sb = StringBuilder(length() - (lastIndex - firstIndex))
sb.append(this, 0, firstIndex)
sb.append(this, lastIndex, length())
return sb.toString()
}
/**
* Removes the part of a string at the given [range].
*
* The end index of the [range] is included in the removed part.
*/
public fun String.removeRange(range: IntRange): String = removeRange(range.start, range.end + 1)
/**
* Replace part of string before the first occurrence of given delimiter with the [replacement] string.
* If the string does not contain the delimiter, returns [missingDelimiterValue] which defaults to the original string.