@Test public void shouldReturnWord() { String text = "Blah 123 Foobar"; String actual1 = WordUtils.getWordAtIndex(text, 8); String actual2 = WordUtils.getWordAtIndex(text, 7); String actual3 = WordUtils.getWordAtIndex(text, 6); String actual4 = WordUtils.getWordAtIndex(text, 5); String actual5 = WordUtils.getWordAtIndex(text, 4); String actual6 = WordUtils.getWordAtIndex(text, 3); String actual7 = WordUtils.getWordAtIndex(text, 2); String actual8 = WordUtils.getWordAtIndex(text, 1); String actual9 = WordUtils.getWordAtIndex(text, 0); String actual10 = WordUtils.getWordAtIndex(text, 9); String actual11 = WordUtils.getWordAtIndex(text, 14); String actual12 = WordUtils.getWordAtIndex(text, 15); assertThat(actual1).isEqualTo("123"); assertThat(actual2).isEqualTo("12"); assertThat(actual3).isEqualTo("1"); assertThat(actual4).isEqualTo(""); assertThat(actual5).isEqualTo("Blah"); assertThat(actual6).isEqualTo("Bla"); assertThat(actual7).isEqualTo("Bl"); assertThat(actual8).isEqualTo("B"); assertThat(actual9).isEqualTo(""); assertThat(actual10).isEqualTo(""); assertThat(actual11).isEqualTo("Fooba"); assertThat(actual12).isEqualTo("Foobar"); }
public static String getWordAtIndex(String text, Integer position) { int start = position; for (; start > 0; start--) { if (Character.isWhitespace(text.charAt(start - 1))) { break; } } if (start == position) { return StringUtils.EMPTY; } return text.substring(start, position); }
WordUtils { public static String getWordAtIndex(String text, Integer position) { int start = position; for (; start > 0; start--) { if (Character.isWhitespace(text.charAt(start - 1))) { break; } } if (start == position) { return StringUtils.EMPTY; } return text.substring(start, position); } }
WordUtils { public static String getWordAtIndex(String text, Integer position) { int start = position; for (; start > 0; start--) { if (Character.isWhitespace(text.charAt(start - 1))) { break; } } if (start == position) { return StringUtils.EMPTY; } return text.substring(start, position); }  }
WordUtils { public static String getWordAtIndex(String text, Integer position) { int start = position; for (; start > 0; start--) { if (Character.isWhitespace(text.charAt(start - 1))) { break; } } if (start == position) { return StringUtils.EMPTY; } return text.substring(start, position); }  static String getWordAtIndex(String text, Integer position); }
WordUtils { public static String getWordAtIndex(String text, Integer position) { int start = position; for (; start > 0; start--) { if (Character.isWhitespace(text.charAt(start - 1))) { break; } } if (start == position) { return StringUtils.EMPTY; } return text.substring(start, position); }  static String getWordAtIndex(String text, Integer position);  }