mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-09-25 03:23:56 +00:00
Fixed #26. Added support for rightPad function
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user