Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I properly format a phone number?

I have a phone number that looks like this:

(def phone_number "1112224444")

When I try this:

(string/format (string/slice phone_number 0 3)
               (string/slice phone_number 3 6) 
               (string/slice phone_number 6 10))

I only get 111

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to get 111-222-4444

>Solution :

You need to use string/format with %s:

(string/format "%s-%s-%s" (string/slice phone_number 0 3) 
                          (string/slice phone_number 3 6)
                          # You can also use 6 -1 (this works no matter the length 
                          # of the string).
                          (string/slice phone_number 6 10)) 

# Output: 111-222-4444
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading