Android Credit Card Check FormatAccountNumber( String UnformattedAccountNumber, int subGroupLength, String Seperator)

Here you can find the source of FormatAccountNumber( String UnformattedAccountNumber, int subGroupLength, String Seperator)

Description

Format Account Number

Declaration

public static String FormatAccountNumber(
            String UnformattedAccountNumber, int subGroupLength,
            String Seperator) 

Method Source Code

//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 FormatAccountNumber(
            String UnformattedAccountNumber, int subGroupLength,
            String Seperator) {//from  w  w w .  j  a v  a 2  s. c  om

        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 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 boolean containsNonNumericCharacters(String rawInput) {
        Pattern p = Pattern.compile("(\\D+)");
        Matcher m = p.matcher(rawInput);
        return m.find();
    }
}

Related

  1. IsDiscoverCreditCard( String UnformattedAccountNumber)
  2. IsJCBCreditCard(String UnformattedAccountNumber)
  3. IsMasterCardCreditCard( String UnformattedAccountNumber)
  4. IsVISACreditCard(String UnformattedAccountNumber)
  5. FindCreditCard(String AccountNumber)
  6. FormatAmericanExpress( String UnformattedAccountNumber, String Seperator)
  7. FormatDinersClub(String UnformattedAccountNumber, String Seperator)