private static String toPrettyString ( long creditCardNumber ) { String plain = Long . toString ( creditCardNumber ) ; // int i = findMatchingRange(creditCardNumber); int length = plain . length ( ) ; switch ( length ) { case 12 : // 12 pattern 3-3-3-3 return plain . substring ( 0 , 3 ) + ' ' + plain . substring ( 3 , 6 ) + ' ' + plain . substring ( 6 , 9 ) + ' ' + plain . substring ( 9 , 12 ) ; case 13 : // 13 pattern 4-3-3-3 return plain . substring ( 0 , 4 ) + ' ' + plain . substring ( 4 , 7 ) + ' ' + plain . substring ( 7 , 10 ) + ' ' + plain . substring ( 10 , 13 ) ; case 14 : // 14 pattern 2-4-4-4 return plain . substring ( 0 , 2 ) + ' ' + plain . substring ( 2 , 6 ) + ' ' + plain . substring ( 6 , 10 ) + ' ' + plain . substring ( 10 , 14 ) ; case 15 : // 15 pattern 3-4-4-4 return plain . substring ( 0 , 3 ) + ' ' + plain . substring ( 3 , 7 ) + ' ' + plain . substring ( 7 , 11 ) + ' ' + plain . substring ( 11 , 15 ) ; case 16 : // 16 pattern 4-4-4-4 return plain . substring ( 0 , 4 ) + ' ' + plain . substring ( 4 , 8 ) + ' ' + plain . substring ( 8 , 12 ) + ' ' + plain . substring ( 12 , 16 ) ; case 17 : // 17 pattern 1-4-4-4-4 return plain . substring ( 0 , 1 ) + ' ' + plain . substring ( 1 , 5 ) + ' ' + plain . substring ( 5 , 9 ) + ' ' + plain . substring ( 9 , 13 ) + ' ' + plain . substring ( 13 , 17 ) ; default : // 0..11, 18+ digits long // plain return plain ; } // end switch }
Convert a creditCardNumber as long to a formatted String . Currently it breaks 16 - digit numbers into groups of 4 .