mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 14:34:04 +00:00
Fixed #13. Added support for leftPad function
This commit is contained in:
@@ -530,9 +530,18 @@ public abstract class Strman {
|
||||
return value.substring(value.length() - n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the beginning of the string is padded.
|
||||
*
|
||||
* @param value The input String
|
||||
* @param pad The pad
|
||||
* @param length Length of the String we want
|
||||
* @return Padded String
|
||||
*/
|
||||
public static String leftPad(final String value, final String pad, final int length) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
if (value.length() >= length) {
|
||||
validate(pad, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
if (value.length() > length) {
|
||||
return value;
|
||||
}
|
||||
return append(Stream.generate(() -> pad).limit(length - value.length()).collect(joining()), value);
|
||||
|
||||
@@ -430,4 +430,14 @@ public class StrmanTest {
|
||||
assertThat(last("", 3), equalTo(""));
|
||||
assertThat(last("f", 3), equalTo("f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftPad_shouldAddPaddingOnTheLeft() throws Exception {
|
||||
assertThat(leftPad("1", "0", 5), equalTo("00001"));
|
||||
assertThat(leftPad("01", "0", 5), equalTo("00001"));
|
||||
assertThat(leftPad("001", "0", 5), equalTo("00001"));
|
||||
assertThat(leftPad("0001", "0", 5), equalTo("00001"));
|
||||
assertThat(leftPad("00001", "0", 5), equalTo("00001"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user