Java Number Format formatCardNumber(String cardNumber)

Here you can find the source of formatCardNumber(String cardNumber)

Description

format Card Number

License

Open Source License

Declaration

public static String formatCardNumber(String cardNumber) 

Method Source Code

//package com.java2s;
/*/*w w  w. j  av a2 s  .com*/
 * eID Middleware Project.
 * Copyright (C) 2010-2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.
 */

public class Main {
    public static String formatCardNumber(String cardNumber) {
        StringBuilder formatted = new StringBuilder();

        if (cardNumber.length() == 10 && cardNumber.startsWith("B")) {
            // B 0123456 78
            formatted.append(cardNumber.substring(0, 1));
            formatted.append(' ');
            formatted.append(cardNumber.substring(1, 8));
            formatted.append(' ');
            formatted.append(cardNumber.substring(8));
        } else if (cardNumber.length() == 12) {
            // 012-3456789-01
            formatted.append(cardNumber.substring(0, 3));
            formatted.append('-');
            formatted.append(cardNumber.substring(3, 10));
            formatted.append('-');
            formatted.append(cardNumber.substring(10));
        } else {
            formatted.append(cardNumber);
        }

        return formatted.toString();
    }
}

Related

  1. format(String number)
  2. formatAmountDigits(String numberString, int length)
  3. formatBinaryNumber(int number, int size)
  4. formatBytes(long number)
  5. formatBytesHumanReadable(float number)
  6. formatCCNumber(String f, String s)
  7. formatCsv(final Number number)
  8. formatFolioNumber(Integer folioNum, String folioMod)
  9. formatFromCurrentPageIndex(int nbItemsPerPage, int totalNumberOfItems, int indexOfCurrentPage)