Apply a mask to String
import javax.swing.text.MaskFormatter; public class Main { public static void main(String args[]) throws Exception { MaskFormatter mf = new MaskFormatter("A-AAAA-AAAA-A"); mf.setValueContainsLiteralCharacters(false); System.out.println(mf.valueToString("123123123123")); } } //1-2312-3123-1