mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-13 00:53:56 +00:00
Added at function
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user