Remove redundant delimiter from end of a string in Kotlin
This post will discuss how to remove the redundant delimiter from the end of a string in Kotlin.
An extra delimiter might get added while iterating over a list and creating a delimited string. For example, the following code appends a redundant comma at the end of the string.
|
1 2 3 4 5 6 7 8 9 |
fun main() { val values = listOf("A", "B", "C") val delimiter = "," var s = "" values.forEach { s += it + delimiter } println(s) // A,B,C, } |
There are several ways to fix this, as covered below:
1. Using substring() function
A simple solution is to use the substring() function to get a substring containing all the characters of the given string except the last.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun removeDelimiter(s: String?): String? { return if (s == null || s.isEmpty()) { s } else s.substring(0, s.length - 1) } fun main() { val values = listOf("A", "B", "C") val delimiter = "," var s = "" values.forEach { s += it + delimiter } println(removeDelimiter(s)) // A,B,C } |
2. Using joinToString() function
In Kotlin, we can avoid looping over the list to get the delimited string. The idea is to invoke the joinToString() function, which returns a new string with the elements of the list joined together using the specified delimiter.
|
1 2 3 4 5 6 7 |
fun main() { val values = listOf("A", "B", "C") val delimiter = "," val result = values.joinToString(delimiter) println(result) // A,B,C } |
Output:
A,B,C
3. Using replaceFirst() function
To remove the last character from the end of a string, we can either use the replace() or the replaceFirst() function that accepts a regular expression. The following solution uses the regex .$ to match with the last character. This works since . matches with any single character (except the line terminating character), and $ matches the position right after the string’s end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun removeDelimiter(s: String?): String? { return if (s == null || s.isEmpty()) { s } else s.replaceFirst(".$".toRegex(), "") } fun main() { val values = listOf("A", "B", "C") val delimiter = "," var s = "" values.forEach { s += it + delimiter } println(removeDelimiter(s)) // A,B,C } |
4. Using StringBuilder
Finally, we can convert the string into a StringBuilder and invoke its deleteCharAt() function to delete the last character. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun removeDelimiter(s: String): String? { return StringBuilder(s) .deleteCharAt(s.length - 1) .toString() } fun main() { val values = listOf("A", "B", "C") val delimiter = "," var s = "" values.forEach { s += it + delimiter } println(removeDelimiter(s)) // A,B,C } |
That’s all about removing the redundant delimiter from the end of a string in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)