![]() ![]() Before RangeĪssertEquals("is the answer", txt.replaceBefore('i', ""))ĪssertEquals("is the answer", txt.replaceBefore("is", "")) Therefore, in order to achieve the same output, we should use the “0 until 3” syntax. On the other hand, the second example creates an open range, so the ending position is exclusive. Therefore, the first example replaces the first three characters with an empty string. In the first example, the “0.2” creates a closed range (both starting and ending indices are inclusive). Since Kotlin has first-class support for ranges, we can use the IntRange syntax using the replaceRange(intRange, replacement) extension function: assertEquals("is the answer", txt.replaceRange(0.2, ""))ĪssertEquals("is the answer", txt.replaceRange(0 until 3, "")) ![]() Therefore, we’re basically replacing the first three characters with an empty string in this example. Val replaced = txt.replaceRange(0, 3, "")Īs usual, the startIndex is inclusive, and the endIndex is exclusive. In order to replace a range of characters, we can use the replaceRange(startIndex, endIndex, replacement) extension function: val txt = "42 is the answer" ![]()
0 Comments
Leave a Reply. |