Here you can find the source of formatCEP(String str)
public static String formatCEP(String str)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import javax.swing.text.MaskFormatter; public class Main { public static String formatCEP(String str) { str = str.replaceAll("[\\D]", ""); try {//w w w . j a va 2 s .c o m str = String.format("%08d", Long.parseLong(str)); String pattern = "#####-###"; try { MaskFormatter mask = new MaskFormatter(pattern); mask.setValueContainsLiteralCharacters(false); return mask.valueToString(str); } catch (ParseException e) { throw new RuntimeException(e); } } catch (NumberFormatException e) { throw new RuntimeException(e); } } }