Fixed #26. Added support for rightPad function

This commit is contained in:
Shekhar Gulati
2016-05-21 22:46:11 +05:30
parent 0208aad28e
commit 665cc8f32e
2 changed files with 27 additions and 1 deletions
+17 -1
View File
@@ -542,7 +542,7 @@ public abstract class Strman {
if (value.length() > length) {
return value;
}
return append(Stream.generate(() -> pad).limit(length - value.length()).collect(joining()), value);
return append(repeat(pad, length - value.length()), value);
}
/**
@@ -785,6 +785,22 @@ public abstract class Strman {
return new StringBuilder(value).reverse().toString();
}
/**
* Returns a new string of a given length such that the ending of the string is padded.
*
* @param value The input String
* @param length Max length of String.
* @param pad Character to repeat
* @return Right padded String
*/
public static String rightPad(final String value, String pad, final int length) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
if (value.length() > length) {
return value;
}
return append(value, repeat(pad, length - value.length()));
}
public static String decode(final String value, final int digits, final int radix) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays
+10
View File
@@ -602,4 +602,14 @@ public class StrmanTest {
assertThat(reverse("foo_"), equalTo("_oof"));
assertThat(reverse("f"), equalTo("f"));
}
@Test
public void rightPad_shouldRightPadAString() throws Exception {
assertThat(rightPad("1", "0", 5), equalTo("10000"));
assertThat(rightPad("10", "0", 5), equalTo("10000"));
assertThat(rightPad("100", "0", 5), equalTo("10000"));
assertThat(rightPad("1000", "0", 5), equalTo("10000"));
assertThat(rightPad("10000", "0", 5), equalTo("10000"));
assertThat(rightPad("10000000", "0", 5), equalTo("10000000"));
}
}