Added at function

This commit is contained in:
Shekhar Gulati
2016-05-03 20:05:19 +05:30
parent d7910e724e
commit e6d4baa0b8
3 changed files with 45 additions and 4 deletions
+22 -1
View File
@@ -1,5 +1,6 @@
package strman;
import java.util.Optional;
import java.util.StringJoiner;
/**
@@ -26,7 +27,7 @@ public interface Strman {
* @param appends an array of strings to append
* @return full String
*/
static String appendArray(String value, String[] appends) {
static String appendArray(final String value, final String[] appends) {
if (value == null) {
throw new IllegalArgumentException("'value' should be not null.");
}
@@ -39,4 +40,24 @@ public interface Strman {
}
return value + joiner.toString();
}
/**
* Get the character at index. This method will take care of negative indexes.
* The valid value of index is between -(length-1) to (length-1).
* For values which don't fall under this range Optional.empty will be returned.
*
* @param value input value
* @param index location
* @return an Optional String if found else empty
*/
static Optional<String> at(final String value, int index) {
if (value == null || value.isEmpty()) {
return Optional.empty();
}
int length = value.length();
if (index < 0) {
index = length + index;
}
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
}
}