Is there any way I can format a string into a specific pattern using regex or is stringbuilder + substring a faster approach?
For example, say a phone number –> 1234567890 as input
And get output as –> (123) 456-7890
I saw it is possible on this article : https://web.archive.org/web/20211020111604/https://www.4guysfromrolla.com/webtech/031302-1.shtml but the given explanation is in ASP. How do I do it in Java ???
One goes for
REwhen same can not be done usingsubstringor is more difficult to do so.In your case better to just use
StringBuilderandinsert()Assuming phone number length validation is in place (=10 chars)
Output