diff --git a/src/main/java/org/apache/commons/codec/language/Nysiis.java b/src/main/java/org/apache/commons/codec/language/Nysiis.java index 36b4241249..9ad16f9497 100644 --- a/src/main/java/org/apache/commons/codec/language/Nysiis.java +++ b/src/main/java/org/apache/commons/codec/language/Nysiis.java @@ -275,7 +275,8 @@ public String nysiis(String str) { // First character of key = first character of name. final StringBuilder key = new StringBuilder(str.length()); - key.append(str.charAt(0)); + final char firstChar = str.charAt(0); + key.append(firstChar); // Transcode remaining characters, incrementing by one character each time final char[] chars = str.toCharArray(); @@ -314,6 +315,12 @@ public String nysiis(String str) { if (lastChar == 'A') { key.deleteCharAt(key.length() - 1); } + + if (key.length() == 0) { + // We've removed the first character of the string. Likely because it was an S or A + // We should return at least the first character + key.append(firstChar); + } } final String string = key.toString(); diff --git a/src/test/java/org/apache/commons/codec/language/NysiisTest.java b/src/test/java/org/apache/commons/codec/language/NysiisTest.java index d13525a8e0..0ae3972438 100644 --- a/src/test/java/org/apache/commons/codec/language/NysiisTest.java +++ b/src/test/java/org/apache/commons/codec/language/NysiisTest.java @@ -140,7 +140,8 @@ public void testDropBy() throws EncoderException { new String[] { "JILES", "JAL" }, // violates 6: if the last two characters are AY, remove A new String[] { "CARRAWAY", "CARY" }, // Original: CARAY - new String[] { "YAMADA", "YANAD" }); + new String[] { "YAMADA", "YANAD" }, + new String[] { "ASH", "A"}); } @Test