Fixed #29. Added support for shuffle function

This commit is contained in:
Shekhar Gulati
2016-05-22 16:55:55 +05:30
parent 95a0be5c7c
commit 0929da6512
2 changed files with 31 additions and 12 deletions
+20 -10
View File
@@ -96,17 +96,8 @@ public abstract class Strman {
* @return character array
*/
public static String[] chars(final String value) {
/**
* The other implementation of this could be using String's split method
* String[] chars = value.split("")
*/
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
int length = value.length();
String[] result = new String[length];
for (int i = 0; i < length; i++) {
result[i] = at(value, i).get();
}
return result;
return value.split("");
}
@@ -908,6 +899,25 @@ public abstract class Strman {
return html.chars().mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase()).map(e -> HtmlEntities.encodedEntities.get(e)).collect(joining());
}
/**
* It returns a string with its characters in random order.
*
* @param value The input String
* @return The shuffled String
*/
public static String shuffle(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
String[] chars = chars(value);
Random random = new Random();
for (int i = 0; i < chars.length; i++) {
int r = random.nextInt(chars.length);
String tmp = chars[i];
chars[i] = chars[r];
chars[r] = tmp;
}
return Arrays.stream(chars).collect(joining());
}
public static String decode(final String value, final int digits, final int radix) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays