I tried to solve the leetcode issue (1694. Reformat Phone Number) using kotlin. You can find the question here

class Solution {
    fun reformatNumber(num: String): String {
        
        var number = StringBuilder()

        for (s in num) {
            if (s.isDigit()) {
                number.append(s)
            }
        }

        var newNumber = StringBuilder()

        for ((index, s) in number.withIndex()) {
            newNumber.append(s)
            if (index % 3 == 2) {
                newNumber.append('-')
            }
        }

        if (number.length > 3) {
            if (newNumber[newNumber.length - 1] == '-') {
                newNumber = newNumber.deleteCharAt(newNumber.length - 1)
            }

            if (newNumber[newNumber.length - 2] == '-') {
                newNumber.setCharAt(newNumber.length - 2 , newNumber[newNumber.length - 3])
                newNumber.setCharAt(newNumber.length - 3 , '-')
            }
        }

        return newNumber.toString()
    }
}