mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-09-27 05:23:57 +00:00
Fixed #11. Added support for last function
This commit is contained in:
@@ -515,6 +515,21 @@ public abstract class Strman {
|
||||
return Objects.equals(value, value.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the last n chars of String
|
||||
*
|
||||
* @param value The input String
|
||||
* @param n Number of chars to return
|
||||
* @return n Last characters
|
||||
*/
|
||||
public static String last(final String value, int n) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
if (n > value.length()) {
|
||||
return value;
|
||||
}
|
||||
return value.substring(value.length() - n);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@@ -422,4 +422,12 @@ public class StrmanTest {
|
||||
assertThat(isUpperCase("Foo"), equalTo(false));
|
||||
assertThat(isUpperCase("foobarfooA"), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void last_shouldReturnLastNChars() throws Exception {
|
||||
assertThat(last("foo", 3), equalTo("foo"));
|
||||
assertThat(last("foobarfoo", 3), equalTo("foo"));
|
||||
assertThat(last("", 3), equalTo(""));
|
||||
assertThat(last("f", 3), equalTo("f"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user