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:
@@ -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.
|
* 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 {
|
public fun String.replaceRange(range: IntRange, replacement: String): String = replaceRange(range.start, range.end + 1, replacement)
|
||||||
if (range.end < range.start)
|
|
||||||
throw IndexOutOfBoundsException("Last index (${range.start}) is less than first index (${range.end})")
|
/**
|
||||||
val sb = StringBuilder()
|
* Removes the part of a string at a given range.
|
||||||
sb.append(this, 0, range.start)
|
* @param firstIndex the index of the first character to be removed.
|
||||||
sb.append(replacement)
|
* @param lastIndex the index of the first character after the removed part to keep in the string.
|
||||||
sb.append(this, range.end, length)
|
*
|
||||||
|
* [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()
|
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.
|
* 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.
|
* If the string does not contain the delimiter, returns [missingDelimiterValue] which defaults to the original string.
|
||||||
|
|||||||
@@ -72,13 +72,31 @@ class StringTest {
|
|||||||
test fun replaceRange() {
|
test fun replaceRange() {
|
||||||
val s = "sample text"
|
val s = "sample text"
|
||||||
assertEquals("sa??e text", s.replaceRange(2, 5, "??"))
|
assertEquals("sa??e text", s.replaceRange(2, 5, "??"))
|
||||||
assertEquals("sa??e text", s.replaceRange(2..5, "??"))
|
assertEquals("sa?? text", s.replaceRange(2..5, "??"))
|
||||||
fails {
|
fails {
|
||||||
s.replaceRange(5..2, "??")
|
s.replaceRange(5..2, "??")
|
||||||
}
|
}
|
||||||
fails {
|
fails {
|
||||||
s.replaceRange(5, 2, "??")
|
s.replaceRange(5, 2, "??")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// symmetry with indices
|
||||||
|
assertEquals("??", s.replaceRange(s.indices, "??"))
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun removeRange() {
|
||||||
|
val s = "sample text"
|
||||||
|
assertEquals("sae text", s.removeRange(2, 5))
|
||||||
|
assertEquals("sa text", s.removeRange(2..5))
|
||||||
|
|
||||||
|
assertEquals(s, s.removeRange(2,2))
|
||||||
|
|
||||||
|
// symmetry with indices
|
||||||
|
assertEquals("", s.removeRange(s.indices))
|
||||||
|
|
||||||
|
// symmetry with replaceRange
|
||||||
|
assertEquals(s.replaceRange(2, 5, ""), s.removeRange(2, 5))
|
||||||
|
assertEquals(s.replaceRange(2..5, ""), s.removeRange(2..5))
|
||||||
}
|
}
|
||||||
|
|
||||||
test fun substringDelimited() {
|
test fun substringDelimited() {
|
||||||
|
|||||||
Reference in New Issue
Block a user