@Test public void separateCardNumberGroups_withAmex_returnsCorrectCardGroups() { String testCardNumber = "378282246310005"; String[] groups = ViewUtils.separateCardNumberGroups(testCardNumber, Card.AMERICAN_EXPRESS); assertEquals(3, groups.length); assertEquals("3782", groups[0]); assertEquals("822463", groups[1]); assertEquals("10005", groups[2]); }
@NonNull static String[] separateCardNumberGroups(@NonNull String spacelessCardNumber, @NonNull @Card.CardBrand String brand) { if (spacelessCardNumber.length() > 16) { spacelessCardNumber = spacelessCardNumber.substring(0, 16); } String[] numberGroups; if (brand.equals(Card.AMERICAN_EXPRESS)) { numberGroups = new String[3]; int length = spacelessCardNumber.length(); int lastUsedIndex = 0; if (length > 4) { numberGroups[0] = spacelessCardNumber.substring(0, 4); lastUsedIndex = 4; } if (length > 10) { numberGroups[1] = spacelessCardNumber.substring(4, 10); lastUsedIndex = 10; } for (int i = 0; i < 3; i++) { if (numberGroups[i] != null) { continue; } numberGroups[i] = spacelessCardNumber.substring(lastUsedIndex); break; } } else { numberGroups = new String[4]; int i = 0; int previousStart = 0; while((i + 1) * 4 < spacelessCardNumber.length()) { String group = spacelessCardNumber.substring(previousStart, (i + 1) * 4); numberGroups[i] = group; previousStart = (i + 1) * 4; i++; } numberGroups[i] = spacelessCardNumber.substring(previousStart); } return numberGroups; }
ViewUtils { @NonNull static String[] separateCardNumberGroups(@NonNull String spacelessCardNumber, @NonNull @Card.CardBrand String brand) { if (spacelessCardNumber.length() > 16) { spacelessCardNumber = spacelessCardNumber.substring(0, 16); } String[] numberGroups; if (brand.equals(Card.AMERICAN_EXPRESS)) { numberGroups = new String[3]; int length = spacelessCardNumber.length(); int lastUsedIndex = 0; if (length > 4) { numberGroups[0] = spacelessCardNumber.substring(0, 4); lastUsedIndex = 4; } if (length > 10) { numberGroups[1] = spacelessCardNumber.substring(4, 10); lastUsedIndex = 10; } for (int i = 0; i < 3; i++) { if (numberGroups[i] != null) { continue; } numberGroups[i] = spacelessCardNumber.substring(lastUsedIndex); break; } } else { numberGroups = new String[4]; int i = 0; int previousStart = 0; while((i + 1) * 4 < spacelessCardNumber.length()) { String group = spacelessCardNumber.substring(previousStart, (i + 1) * 4); numberGroups[i] = group; previousStart = (i + 1) * 4; i++; } numberGroups[i] = spacelessCardNumber.substring(previousStart); } return numberGroups; } }
ViewUtils { @NonNull static String[] separateCardNumberGroups(@NonNull String spacelessCardNumber, @NonNull @Card.CardBrand String brand) { if (spacelessCardNumber.length() > 16) { spacelessCardNumber = spacelessCardNumber.substring(0, 16); } String[] numberGroups; if (brand.equals(Card.AMERICAN_EXPRESS)) { numberGroups = new String[3]; int length = spacelessCardNumber.length(); int lastUsedIndex = 0; if (length > 4) { numberGroups[0] = spacelessCardNumber.substring(0, 4); lastUsedIndex = 4; } if (length > 10) { numberGroups[1] = spacelessCardNumber.substring(4, 10); lastUsedIndex = 10; } for (int i = 0; i < 3; i++) { if (numberGroups[i] != null) { continue; } numberGroups[i] = spacelessCardNumber.substring(lastUsedIndex); break; } } else { numberGroups = new String[4]; int i = 0; int previousStart = 0; while((i + 1) * 4 < spacelessCardNumber.length()) { String group = spacelessCardNumber.substring(previousStart, (i + 1) * 4); numberGroups[i] = group; previousStart = (i + 1) * 4; i++; } numberGroups[i] = spacelessCardNumber.substring(previousStart); } return numberGroups; }  }
ViewUtils { @NonNull static String[] separateCardNumberGroups(@NonNull String spacelessCardNumber, @NonNull @Card.CardBrand String brand) { if (spacelessCardNumber.length() > 16) { spacelessCardNumber = spacelessCardNumber.substring(0, 16); } String[] numberGroups; if (brand.equals(Card.AMERICAN_EXPRESS)) { numberGroups = new String[3]; int length = spacelessCardNumber.length(); int lastUsedIndex = 0; if (length > 4) { numberGroups[0] = spacelessCardNumber.substring(0, 4); lastUsedIndex = 4; } if (length > 10) { numberGroups[1] = spacelessCardNumber.substring(4, 10); lastUsedIndex = 10; } for (int i = 0; i < 3; i++) { if (numberGroups[i] != null) { continue; } numberGroups[i] = spacelessCardNumber.substring(lastUsedIndex); break; } } else { numberGroups = new String[4]; int i = 0; int previousStart = 0; while((i + 1) * 4 < spacelessCardNumber.length()) { String group = spacelessCardNumber.substring(previousStart, (i + 1) * 4); numberGroups[i] = group; previousStart = (i + 1) * 4; i++; } numberGroups[i] = spacelessCardNumber.substring(previousStart); } return numberGroups; }   }
ViewUtils { @NonNull static String[] separateCardNumberGroups(@NonNull String spacelessCardNumber, @NonNull @Card.CardBrand String brand) { if (spacelessCardNumber.length() > 16) { spacelessCardNumber = spacelessCardNumber.substring(0, 16); } String[] numberGroups; if (brand.equals(Card.AMERICAN_EXPRESS)) { numberGroups = new String[3]; int length = spacelessCardNumber.length(); int lastUsedIndex = 0; if (length > 4) { numberGroups[0] = spacelessCardNumber.substring(0, 4); lastUsedIndex = 4; } if (length > 10) { numberGroups[1] = spacelessCardNumber.substring(4, 10); lastUsedIndex = 10; } for (int i = 0; i < 3; i++) { if (numberGroups[i] != null) { continue; } numberGroups[i] = spacelessCardNumber.substring(lastUsedIndex); break; } } else { numberGroups = new String[4]; int i = 0; int previousStart = 0; while((i + 1) * 4 < spacelessCardNumber.length()) { String group = spacelessCardNumber.substring(previousStart, (i + 1) * 4); numberGroups[i] = group; previousStart = (i + 1) * 4; i++; } numberGroups[i] = spacelessCardNumber.substring(previousStart); } return numberGroups; }    }