Skip to content

Commit

Permalink
addition of filledString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed Oct 13, 2024
1 parent 90c1b59 commit 8d2a029
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,22 @@ public static boolean equalsIgnoreCase(final CharSequence cs1, final CharSequenc
return Strings.CI.equals(cs1, cs2);
}

/**
* Constructs a string of specified length filled with the specified char.
* @param length the length of the final string.
* @param fillChar the character to file it will.
* @return A string of specified length filled with the specified char.
* @since 1.10.0
*/
public static String filledString(final int length, final char fillChar) {
if (length < 0) {
throw new IllegalArgumentException("Length must not be negative");
}
final char[] padding = new char[length];
Arrays.fill(padding, fillChar);
return new String(padding);
}

/**
* Returns the first value in the array which is not empty (""),
* {@code null} or whitespace only.
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,13 @@ public void testEscapeSurrogatePairsLang858() {
assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeEcmaScript("\uDBFF\uDFFD")); //fail LANG-858
}

@Test
public void testFilledString() {
assertEquals("-----", StringUtils.filledString(5, '-'));
assertEquals("", StringUtils.filledString(0, '-'));
assertThrows(IllegalArgumentException.class, () -> StringUtils.filledString(-1, '-'));
}

@Test
public void testGeorgianSample() {
final char[] arrayI = {
Expand Down

0 comments on commit 8d2a029

Please sign in to comment.