How do I shuffle the characters in a string (e.g. hello could be ehlol or lleoh or ...). I don\'t want to use the Collections.shuffle(...) method, is there anyt
Not sure why you wouldn't want to use shuffle, unless it's for school. ;)
And if you're concerned with performance, you definitely can't use any solution that concatenates strings with "+".
Here's the most compact solution I could come up with:
public static String shuffle(String string) {
if (StringUtils.isBlank(string) {
return string;
}
final List randomChars = new ArrayList<>();
CollectionUtils.addAll(randomChars, ArrayUtils.toObject(string.toCharArray()));
Collections.shuffle(randomChars);
return StringUtils.join(randomChars, "");
}