Complete the code to use the with function to print the length of the string.
val text = "Hello" with([1]) { println(length) }
The with function takes an object as its first argument. Here, text is the string object we want to use inside the block.
Complete the code to use with to build a string and return its length.
val result = with(StringBuilder()) { append("Hi") append(" there") [1] }
toString() without getting length.size which is not a StringBuilder property.Inside with, this is the StringBuilder. To get the length of the built string, use length.
Fix the error in the code by completing the with block to correctly modify the list.
val numbers = mutableListOf(1, 2, 3) with(numbers) { add(4) [1] }
remove which deletes elements instead of adding.clear which empties the list.To add multiple elements to the list inside with, use addAll with a list of new elements.
Fill both blanks to create a with block that modifies a StringBuilder and returns the final string.
val result = with(StringBuilder()) { [1]("Hello") [2](" World") toString() }
insert which adds text at a specific position.delete or replace which modify existing text.To add text to a StringBuilder, use append. Both blanks need append to build the string.
Fill all three blanks to use with to configure a MutableList and return its size.
val list = mutableListOf<Int>() val size = with(list) { [1](10) [2](20) [3] }
clear which empties the list.length which is not a list property.Use add twice to add elements, then size to get the number of elements in the list.