I get a string containing a phone number as input. This string looks like this:
79998886666
But I need to convert it to this form:
+7 999 888-66-66
I tried several ways, but the most recent one I got looks like this:
private fun formatPhone(phone: String): String {
val prefix = "+"
val countryCode = phone.first()
val regionCode = phone.dropLast(7).drop(1)
val firstSub = phone.dropLast(4).drop(4)
val secondSub = phone.dropLast(2).drop(7)
val thirdSub = phone.drop(9)
return "$prefix$countryCode $regionCode $firstSub-$secondSub-$thirdSub"
}
But it seems to me that this method looks rather strange and does not work very efficiently. How can this problem be solved differently?