Java tutorial
//package com.java2s; public class Main { public static String formatCardId(String cardId) { StringBuffer sb = new StringBuffer(); if (cardId != null && cardId.length() > 0) { for (int i = 0; i < cardId.length(); i++) { sb.append(cardId.substring(i, i + 1)); if (i >= 3 && (i - 3) % 4 == 0 && i != cardId.length() - 1) { sb.append(" "); } } } return sb.toString(); } }