mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 14:34:04 +00:00
committed by
Shekhar Gulati
parent
605eb4f2a5
commit
84b3303718
@@ -34,6 +34,9 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.function.Function.identity;
|
||||
import static java.util.stream.Collectors.counting;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
/**
|
||||
@@ -87,7 +90,7 @@ public abstract class Strman {
|
||||
* @return an Optional String if found else empty
|
||||
*/
|
||||
public static Optional<String> at(final String value, int index) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
if (isNullOrEmpty(value)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
int length = value.length();
|
||||
@@ -97,6 +100,10 @@ public abstract class Strman {
|
||||
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
|
||||
}
|
||||
|
||||
private static boolean isNullOrEmpty(String input) {
|
||||
return input == null || input.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with strings between start and end.
|
||||
*
|
||||
@@ -1302,4 +1309,20 @@ public abstract class Strman {
|
||||
}
|
||||
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of occurrences of each character in the string
|
||||
*
|
||||
* @param input The input string
|
||||
* @return A map containing the number of occurrences of each character in the string
|
||||
*/
|
||||
public static Map<Character, Long> charsCount(String input) {
|
||||
if (isNullOrEmpty(input)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return input.chars()
|
||||
.mapToObj(c -> (char) c)
|
||||
.collect(groupingBy(identity(), counting()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user