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 transform "48a493d902b5" into "48:a4:93:d9:02:b5" in Java?

I have a string "48a493d902b5", but I need to convert it in "48:a4:93:d9:02:b5". I could build a While structure but i don’t know if it is the better aproach.

Is it possible use Regex for it? If so, How can I do it?

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

>Solution :

Yes, it’s possible use a regex. This expression (.{2}) splits the String in pairs of characters using the lookBehind (?<=\G.{2}) and in the sequence inserts ":" between this pairs, except for the last pair (?!$).

    String originalString = "48a493d902b5";
    String formatedString = originalString.replaceAll("(?<=\\G.{2})(?!$)", ":");
    System.out.println(formatedString);
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