Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addition of filledString() method #1295

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading