Search code examples
androidkotlinandroid-edittext

Characters does not replace in string


I have a phone number like this

+43 0431 15903412

But at my EditText this is the input I give

431 903412

The only thing I need is to add a 0 before 4 and 15 before 9, is there any regex or simple way of adding this without looping through all the characters in my string and replacing by index ?

I did this to add the +43 0

val modifiedPhoneNumber = "+430$phoneNumber"

But now I need to add the 15 before 9 and I dont know how

Thanks


Solution

  • as i understand you need +43 0431 15903412 from 431 903412 it can be done as follow using StringBuilder

     var j = 431903412
            var x = Integer.toString(j)
            x = StringBuilder(x).insert(x.length - 9, "+430").insert(x.length - 6, "15").toString()
            Log.e("Show Updated Strin", x)
    

    using this you will get +43150431903412