Here you can find the source of FormatDinersClub(String UnformattedAccountNumber, String Seperator)
public static String FormatDinersClub(String UnformattedAccountNumber, String Seperator)
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static final String DEFAULT_SEPERATOR = "-"; public static String FormatDinersClub(String UnformattedAccountNumber, String Seperator) {/*from ww w. j a va 2 s . c o m*/ //Sample format: 3056-930902-5904 [4 - 6 - 4 for a total of 14 digits] if (UnformattedAccountNumber == null) { return ""; } UnformattedAccountNumber = UnFormatAccountNumber(UnformattedAccountNumber); UnformattedAccountNumber = UnformattedAccountNumber.trim(); if (UnformattedAccountNumber.length() == 0) { return ""; } if (Seperator == null) { Seperator = DEFAULT_SEPERATOR; } String formattedAccountNumber = ""; StringBuilder sb = new StringBuilder(); if (UnformattedAccountNumber.length() == 14) { sb.append(UnformattedAccountNumber.substring(0, 4)); sb.append(Seperator); sb.append(UnformattedAccountNumber.substring(4, 10)); sb.append(Seperator); sb.append(UnformattedAccountNumber.substring(10, 14)); formattedAccountNumber = sb.toString(); } else { formattedAccountNumber = FormatAccountNumber( UnformattedAccountNumber, 4, Seperator); } return formattedAccountNumber; } public static String UnFormatAccountNumber(String AccountNumber) { /* StringBuilder sb = new StringBuilder(); Pattern p = Pattern.compile("(\\d+)"); Matcher m = p.matcher(AccountNumber); while (m.find()) { sb.append(m.group(1)); } return sb.toString();*/ String result = AccountNumber; if (containsNonNumericCharacters(AccountNumber)) { StringBuilder sb = new StringBuilder(); Pattern p = Pattern.compile("(\\d+)"); Matcher m = p.matcher(AccountNumber); while (m.find()) { sb.append(m.group(1)); } result = sb.toString(); } return result; } public static String FormatAccountNumber( String UnformattedAccountNumber, int subGroupLength, String Seperator) { if (UnformattedAccountNumber == null) { return ""; } UnformattedAccountNumber = UnFormatAccountNumber(UnformattedAccountNumber); UnformattedAccountNumber = UnformattedAccountNumber.trim(); if (UnformattedAccountNumber.length() == 0) { return ""; } if (Seperator == null) { Seperator = DEFAULT_SEPERATOR; } if (subGroupLength < 1) { subGroupLength = 4; } int end = 0; StringBuilder result = new StringBuilder(); for (int start = 0; start < UnformattedAccountNumber.length(); start += subGroupLength) { end = start + subGroupLength; if (end > UnformattedAccountNumber.length()) { end = UnformattedAccountNumber.length(); } result.append(UnformattedAccountNumber.substring(start, end)); result.append(Seperator); } // remove the last Seperator String formattedAccountNumber = result.toString(); formattedAccountNumber = formattedAccountNumber.substring(0, formattedAccountNumber.length() - Seperator.length()); return formattedAccountNumber; } public static boolean containsNonNumericCharacters(String rawInput) { Pattern p = Pattern.compile("(\\D+)"); Matcher m = p.matcher(rawInput); return m.find(); } }